java - Hibernate cannot simultaneously fetch multiple bags -
hibernate throws exception during sessionfactory creation:
org.hibernate.loader.multiplebagfetchexception: cannot simultaneously fetch multiple bags
this test case:
parent.java
@entity public parent { @id @generatedvalue(strategy=generationtype.identity) private long id; @onetomany(mappedby="parent", fetch=fetchtype.eager) // @indexcolumn(name="index_col") if had problem solve retrieve more children have, 1 child null. private list<child> children; }
child.java
@entity public child { @id @generatedvalue(strategy=generationtype.identity) private long id; @manytoone private parent parent; }
how problem? can do?
edit
ok, problem have "parent" entity inside parent, real behavior this:
parent.java
@entity public parent { @id @generatedvalue(strategy=generationtype.identity) private long id; @manytoone private antoherparent anotherparent; @onetomany(mappedby="parent", fetch=fetchtype.eager) private list<child> children; }
anotherparent.java
@entity public antoherparent { @id @generatedvalue(strategy=generationtype.identity) private long id; @onetomany(mappedby="parent", fetch=fetchtype.eager) private list<anotherchild> anotherchildren; }
hibernate doesn't 2 collections fetchtype.eager
, seems bug, i'm not doing unusual things...
removing fetchtype.eager
parent
or anotherparent
solves problem, need it, real solution use @lazycollection(lazycollectionoption.false)
instead of fetchtype
(thanks bozho solution).
i think newer version of hibernate (supporting jpa 2.0) should handle this. otherwise can work around annotating collection fields with:
@lazycollection(lazycollectionoption.false)
remember remove fetchtype
attribute @*tomany
annotation.
but note in cases set<child>
more appropriate list<child>
, unless need list
- go set
Comments
Post a Comment