java - Hibernate fetch only single child instance -
java - Hibernate fetch only single child instance -
seems standard problem, searching solution have no success. have parent class person 2 childen sets:
class person { @onetomany(mappedby = "person", cascade = cascadetype.all, orphanremoval = true, fetch = fetchtype.lazy) set<dog> dogs; @onetomany(mappedby = "person", cascade = cascadetype.all, orphanremoval = true, fetch = fetchtype.lazy) set<cat> cats; } child class cat (class dog looks same):
class cat { private string name; @manytoone(targetentity = person.class, fetch = fetchtype.eager) @joincolumn(name = "person_id") public person getperson() { homecoming person; } } what problem. when seek retrieve persons, cat name equals 'fritz', hibernate create joined query on table cat, name 'fritz'. looks fine, when expand retrieved person (in thought debugger), contains only single instance of cat (with name 'fritz'), despite person can have multiple cats. simultaneously expanding, hibernate create other request dog table , retrieve items linked person, , field populates correctly. how can forcefulness hibernate populate field cat items, not joined item?
p.s. utilize spring's hibernatetemplate session.get(), not hql.
edit 1. generated query (the sec part executes when expand retrieved person in debugger):
hibernate: select person.person_id, person.person_name, cat.cat_id, cat.name, person person left outer bring together cat cat on person.person_id=cat.person_id cat.name='fritz' hibernate: select dog.dog_id, dog.name, dog.person_id dog dog dog.dog_id=1 edit 2. seems issue former hibernate bug . i'm still need help. hibernate version 4.3.
i not confident in using debugger see how hibernate maps association. uses special collections populates on-demand, because populating collection causes request , hibernate tries give mix between simplicity , performance.
so when inquire *the person owning pussicat`, hibernate query on table person unless eagerly load kid table, queries on kid tables if list cats , dogs of person in code not in debugger.
and know cannot eagerly load association. if did, query load database ...
imho when hibernate did not populate cats set, because in debugger set not null nor empty , the debugger not inquire hibernate issue query. if asked in code list of cats, correctly.
java hibernate
Comments
Post a Comment