This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JBoss/WildFly and the fly likes Problem in persistence.xml: org.hibernate.ejb.HibernatePersistence cannot be cast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Problem in persistence.xml: org.hibernate.ejb.HibernatePersistence cannot be cast" Watch "Problem in persistence.xml: org.hibernate.ejb.HibernatePersistence cannot be cast" New topic
Author

Problem in persistence.xml: org.hibernate.ejb.HibernatePersistence cannot be cast

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Hi,

I'm using JBoss 4.2.2 and building an application with Hibernate 4.0.1.Final. I'm confused about how to setup my persistence.xml provider class. Currently, in my JAR's META-INF directory, I have this persistence.xml



but upon deploying my JAR to JBoss, I'm greeted with this error



What class should I be using for the provider given my app server and Hibernate version? - Dave
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Jboss includes its own implementation of JPA they are probably being loaded by separate class loaders.

This link may have some insight (try the last suggestion for configuring the classloaders)
http://stackoverflow.com/questions/3759144/org-hibernate-ejb-hibernatepersistence-cannot-be-cast-to-javax-persistence-spi-p

You could try excluding JPA and using the ones that JBOSS has but with an old version like this I imagine there will be other issues, and you would be using JPA 1.0



Good luck

[How To Ask Questions][Read before you PM me]
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

JBoss 4.x and Hibernate 4.x do NOT work well together. You may get it to work but some core
components of JBoss use it's internal hibernate.

We are stuck with Spring 2.5.6 for this very reason.

WP
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Are you talking about the suggestion from Pascal Thivent or Stephan?

Btw, I tried removing the dependencies related to JPA 2.0 but got another exception ...

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Yeah I figured that would happen

I was talking about Stephan. You can look also at this post it is along the same lines

http://stackoverflow.com/questions/8476944/class-loading-isolation-issue-or-how-to-use-jpa2-on-jboss-5-x

The idea is to try to get jboss to use the classes packaged in your WAR and not the versions it has.
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Thanks for that clarification. I'm going to check that out, but my application is a JAR, not a WAR. I've got through hell trying to figure out how to instruct JBoss to prefer the classes in my JAR instead of its own, but I haven't figured out how. If you know, or have suggestions for another packaging structure, I'm grateful.

- Dave
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

This is not a web-app? What exactly do you mean by deploying a jar?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Bill

An EJB jar is an example of a deployable artifact, it doesn't have to be an EAR or WAR.
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
The only thing in the JAR is a Quartz job that I want to run periodically (There are some JBoss annotations that let me set up the cron). Everything else in the JAR are classes that support that job. Our sysadmin has said I need to do this in JBoss instead of setting up a traditional Unix cron job.

I'm open to other packaging configs, but I don't think WAR is appropriate because I don't have any web components.

Thanks for getting back to me with all the Olympics on, - Dave
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

James Boswell wrote:Bill

An EJB jar is an example of a deployable artifact, it doesn't have to be an EAR or WAR.


Thanks James I had read a little on it awhile back (although I have zero experience with it) however I though that was something that came with the EJB 3 stuff and was newer than JBOSS 4. Anyways I stand corrected


Edit I found this with all the deployment descriptors for a newer version of JBOSS I did not see an equivalent document for 4.

https://docs.jboss.org/author/display/AS71/Deployment+Descriptors+used+In+AS7.1
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
I don't actually have any EJBs so should I still be building an EJB jar? - Dave
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Dave Alvarado wrote:I don't actually have any EJBs so should I still be building an EJB jar? - Dave


Well it has to use some kind of deployment descriptor. Can you look at that link on my last post and tell me which one you are using to deploy your jar?
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
After reading over your link with the deployment descriptors, I'm at a loss. What deployment descriptor is appropriate for my application, in which I just want to run a Quartz job periodically? I appreciate any recommendations although I'm going to see if jboss-client.xml is supported for JBoss 4. - Dave
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Well whatever deployment descriptor you have seems to be working for you as far as deployment is concerned. I was hoping that you could use jboss-classloading.xml to force your application to use the libs that are included with your application but it seems this was added in JBOSS 5. I think I will move this to the JBOSS forum hopefully some JBOSS experts can help you with this.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

The real question is why do you want to use JPA 2 (Hibernate 4.x) in an environment which doesn't support JPA 2 (JBoss AS 4.x)? Why not use Hibernate 3 if you want to stick wth JBoss AS 4.x. If at all you are using any JPA 2 specific features then it really means you need a runtime environment which supports JPA 2. JBoss AS 4.x is not that environment. You'll have to upgrade.

[My Blog] [JavaRanch Journal]
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Ah Jaikiran, I've seen you weigh in on this issue many times. I've wrote my app in Hibernate b/c I didn't think my company was using an app server this out of date. Before I rewrite my app, you above all would know, can JBoss be instructed to use my Hibernate JARs over its own? In my JAR file, I have this file, META-INF/jboss.xml



but JBoss appears to be loading older versions of the Hibernate classes ...



I have verified the proper Hibernate classes are getting included in the JAR. Thanks, - Dave
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Looks like your getting closer

That exception looks like it wants you to use AnnotationConfiguration which is deprecated in the version of hiberate you are using. You are probably using Configuration which is what you should be doing. The only thing I can think is one of the transitive dependencies are still being included by jboss and its not using what you have in your jar. Maybe rather than depending on maven to bundle the transitive dependencies to hibernate-core explicitly define them and verify that each one is in your jar.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in persistence.xml: org.hibernate.ejb.HibernatePersistence cannot be cast
 
Similar Threads
Getting dialect error while deploying EJB
EJB, JPA with MS AQccess Database
deployment problem to JBoss 4.2.1-GA
javax.persistence. TransactionRequiredException
Primary Key Autogeneration for InheritenceType. TABLE_PER_CLASS