jpa

Hibernate "cannot fetch simultaneous bags" Error - on entity with two @ManyToOne fields

Hibernate's implementation of JPA has some quirks, such as not being able to handle the eager fetching of two @OneToMany relationships in a single entity without the use of some work-arounds. Although this article is not about these work-arounds I will give a quick overview of the problem and solution to that problem first, before looking at the specifics of the case where I got the error on a entity with two @ManyToOne.

Toplink and Hibernate differences prt2

Toplink is real sucky. This works in Hibernate but throws an error in Toplink.

"Select s from SalesOrder s where s.status.code not like 'Unconfirmed' and ( s.id in (714,653,-1))"

The error thrown is really stupid. 

" expecting RIGHT_ROUND_BRACKET, found 'in'

Toplink has some way to go to catch up to hibernate.

Toplink and JNDI configuration on Glassfish and Tomcat

JNDI setup and configuration is one of those parts of java where the differences between container implementations are just enough to be a barrier to porting your application to a different server. This combined with the incompatibilities with JPA between implementations can lead to vendor or stack lock-in, hopefully this get better with the next release of JPA.

One of the issues with JPA is using JNDI datasources for your persistenceContext.

Glassfish JNDI setup and persistence.xml

Glassfish does not require any special handlng for its JNDI entry.

Differences between Hibernate JPA and Toplink JPA

On one of our projects we decided to switch JPA providers from Hibernate to Toplink. The main reason for this was that Hibernate depends on so many core open source libraries that other open source projects also use, that there are all kinds of version problems when the application is deployed in a JEE container, such as Tomcat or Glassfish, that uses different versions internally. Basically jar hell with all kinds of issues with classloaders loading the wrong version of the jar.

JPA Detached Object Gotchas

Java Persistence API detached object gotchas

One of the issues to get your head around in both Hibernate and JPA is how to handle detached entities. In Hibernate one has to deal with the session object and in JPA it is called the persistence context.

Cannot find the declaration of element 'persistence'

A common error when using EJB3 is

Cannot find the declaration of element 'persistence'

This is caused by a missing xml namespace attribute declaration in your persistence.xml file. To fix this error make sure your persistence tag has the following syntax:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

instead of

<persistence>