Showing posts with label jpa hibernate programming java. Show all posts
Showing posts with label jpa hibernate programming java. Show all posts

Sunday, March 09, 2008

JPA Error : org.hibernate.HibernateException: cannot simultaneously fetch multiple bags

Apparently JPA has a problem with @OneToMany(fetch=FetchType.EAGER) relationships. Above error seems to occur whenever there is a multiple of such relationships in a class. BUT I had a problem where even a single relationship gave this error. The funny bit is that I had NO problems till I needed to add a new persistant class, thats when this guy popped up. Before that I DID HAVE MULTIPLE @OneToMany(fetch=FetchType.EAGER) relationships and it worked perfectly!

Frustrations!

Morale of the story : Don'y be lazy! Cut down @OneToMany(fetch=FetchType.EAGER) relationships. Anyways, I don't think its THAT efficient anyways

JPA Error (on delete) : java.lang.IllegalArgumentException: Removing a detached instance

This is a problem I got while deleting a record.

What I've figured is that you cannot delete a persistent object that you got very earlier (ex: as a result of a relationship of another object)

What's needed to be done is to refetch that object again by using find and delete it.

Solution:
EntityManager em=CWUtil.getEntityManager();
em.getTransaction().begin();
m=em.find(Membership.class, m.getId()); //<-Fetched again here
em.remove(m); //<-Deleted here
em.getTransaction().commit();
em.close();


Solves the problem!