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).
Comments
Post a Comment