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.