hibernate - CDI + JPA Multiple @OneToMany - LAZY/EAGER - LazyInitializationException OR Cannot instantiate multiple bags -


i developing application using jboss 7, cdi, jpa, , other resources bundled.

my question is: when create project in eclipse using maven-jboss-webapp-archetype generates files, have been extensively studying , trying along with. problem see myself little bit confused whether using hibernate resources or jpa resources.

second question: when use multiple @onetomany relationships in same entity notice 2 behaviors:

a) without specifying eager type fetch, deploys application when try use list gives me lazyinitializationexception error vastly discussed here.

b) when specify eager fetchtype on @*tomany relationship doesnt deploys application. gives me error: cannot instantiate multiple bags.

here problematic piece of code:

@onetomany(cascade = cascadetype.all, mappedby = "teamuser" , fetch = fetchtype.eager) private collection<users> userscollection;  @onetomany(cascade = cascadetype.all, mappedby = "teamranking" , fetch = fetchtype.lazy) private collection<ranking> rankingcollection;  @notnull @onetomany(cascade = cascadetype.all, mappedby = "teammodality" , fetch = fetchtype.lazy) private collection<modality> modalitiescollection;  public teams() {  }  public teams(long id_team , string name) {     this.id = id_team;     this.name = name; } 

i have been reading , people told me jpa not support fetchtype.eager multiple realtionships in same entity. need use multiple relationships in entity in specific. don't know anymore, because have tried several approaches, lazy fetching, , of them present kind of issue. example, if leave 1 fetchtype.eager in class deploys app doesn't runs because when try fetch list database gives me error: lazyinitializationexception, , when try put of them using fetchtype.eager doesnt deploy because gives error: cannot instantiate multiple bags.

so issues here related to:

1) maven-jboss-webapp-archetype uses hibernate or jpa or both? 2) how can attack problem of using multiple @onetomany relationships in entity? , although know fetching eagerly collections in model not best approach because when system grows have performance issues right? how can work lazy loading?

first of all, hibernate implementation of jpa pecification, , default 1 in jboss. you're using jpa, hibernate implementation.

second, lazyinitialization exception because access lazy-loaded collection, after having closed session, without having initialized collection while session still open. if have 2 lazy collections , know client code needs both initialized, initialize them before returning entity , closing session (i.e. while you're still inside transactional service method):

someentity e = em.find(someentity.class, someid); e.getfirstcollection().size(); // initialize first collection e.getsecondcollection().size(); // initialize second collection return e; 

you can fetch multiple tomany associations @ once, 1 of them can bag (i.e. collection or list). wouldn't advise eager collections generally. if want them, make them sets instead of collections.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -