-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: cannot simultaneously fetch multiple bags
PostPosted: Mon Mar 10, 2008 2:48 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
I am deploying a Seam-JPA application powered by Hibernate. I have a Widget entity that has a unidirectional 1:M with a PricingEvent entity for pricing history purposes.

Things look like this:

Code:
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)
@JoinColumn(name = "WIDGET_ID", nullable = false)
public List<PricingEvent> getPricingHistory() {
      return pricingHistory;
}


Now when I set the fetch type to eager, I get this:

Code:
org.hibernate.HibernateException: cannot simultaneously fetch multiple bags
   at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:66)
   at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:75)
   at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:43)
   at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:33)
   at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:103)
   at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1748)
   at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1752)
   at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:2984)
   at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:2977)
   at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:690)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:290)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:730)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
   at org.jboss.seam.persistence.EntityManagerFactory.createEntityManagerFactory(EntityManagerFactory.java:81)
   at org.jboss.seam.persistence.EntityManagerFactory.startup(EntityManagerFactory.java:50)
   at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source)
   at java.lang.reflect.Method.invoke(Ljava.lang.Object;[Ljava.lang.Object;I)Ljava.lang.Object;(Unknown Source)
   at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
   at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
   at org.jboss.seam.Component.callComponentMethod(Component.java:2082)
   at org.jboss.seam.Component.callCreateMethod(Component.java:1997)
   at org.jboss.seam.Component.newInstance(Component.java:1968)
   at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304)
   at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278)
   at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:95)


Now there is quite a bit about this on the forums. I have a few questions.

1) Do I have to use a Set or SortedSet? I would rather use a List.
2) Given this link

http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-mapping-association-collections

I find a passage saying

Quote:
A unidirectional one to many using a foreign key column in the owned entity is not that common and not really recommended. We strongly advise you to use a join table for this kind of association (as explained in the next section). This kind of association is described through a @JoinColumn


That appears to be precisely what I am doing. It seems overkill to have an association table for a 1:M relationship, and I would argue that is in fact pretty common to have things set up as I do. Anyway, why is it not recommended? What is the advantage of an association table?

Thanks for any insight.


Top
 Profile  
 
 Post subject: Re: cannot simultaneously fetch multiple bags
PostPosted: Mon Mar 10, 2008 3:00 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Umm, the problem here is that hibernate is detecting multiple Bags that need to be fetched with joins. However, what you have is a list. So you also need to specify @IndexColumn so that hibernate knows it is a list and not a bag. Therefore, you can either specify an index column or use a Set. One another thing you can do is to change from eager fetching to lazy fetching or select fetching.

The suggestion in hibernate document is more of a database design suggestion and you can still have foreign key one-to-many relations. They are indeed common in many applications. However, this is only my idea.



Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.