java - JPA OneToMany eager fetch does not work -


i have weird problem 2 entities one-to-many relation in jpa. using glassfish 3.1.2.2 eclipselink 2.3.2. first entity:

@namedqueries({     @namedquery(name="samplequerygroup.findall", query="select g samplequerygroup g") }) @entity public class samplequerygroup implements serializable {      // simple properties, including id (primary key)      @onetomany(         mappedby = "group",         fetch = fetchtype.eager,         cascade = {cascadetype.remove, cascadetype.merge}     )     private list<samplequery> samplequeries;      // gettes/setters, hashcode/equals  } 

and second one:

@entity public class samplequery implements serializable {      // simple properties, including id (primary key)      @manytoone(cascade = {cascadetype.persist})     private samplequerygroup group;      // gettes/setters, hashcode/equals  } 

i have stateless session bean uses injected entitymanager run samplequerygroup.findall named query. have cdi managed bean calls ssb method , iterates through samplequerygroup.getsamplequeries() each samplequerygroup returned method. didn't paste code pretty straightforward , somehow standard java ee application.

the problem eager fetch not work , getsamplequeries() returns empty list. however, when change fetch type fetchtype.lazy, works , list correctly populated. don't understand why happens. have internal caching mechanisms?

my guess when add new samplequery not adding samplequerygroup samplequeries, when access it, not their. when lazy not trigger until have inserted samplequery, there.

you need maintain both sides of relationships. (you disable caching, or refesh object, code still broken).

see, http://en.wikibooks.org/wiki/java_persistence/relationships#object_corruption.2c_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -