What is the correct syntax for an ejbql query that traverses four levels of many to one relationships
I have a simple data model in JPA (hibernate) consisting of many to one and one to one relationships similar to this:
town -> state -> governor -> nation -> continent
where town is many to one to state, state is one to one to governor, governor is many to one to nation and nation is many to one to continent.
I would like to fetch a single town instance by its unique Id and 开发者_运维问答also eagerly fetch its related state, governor, nation and continent using ejbql. I believe the proper ejbql is:
select t from town t
join fetch t.state s
join fetch s.governor g
join fetch g.nation n
join fetch n.continent c
where t.id=?id
Hiberante generates the correct sql from this ejbql, but when I do myTown.getState().getGovernor(). I get back a null object for my governor. Why is hibernate not populating the governor? It seems like it doesn't want to populate objects more than one level up the tree. Anybody see what I'm doing wrong?
Nevermind. My syntax above does in fact work. My problem was not flushing the hibernate session from some previous activity and getting a cached version of the object that wasn't propertly populated. After correcting that problem, everything is fine.
精彩评论