hibernate - Why this @OneToOne bidirectional relationship gives me a null proxy when querying the owning entity? -
i'm using hibernate 5.2.4. these 2 entities have one-to-one bidirectional relationship:
@entity @table(name="post") public class post { @id @column(name="id", nullable=false) private int id; @column(name="text", nullable=false) private string text; @onetoone(mappedby="post", fetch=fetchtype.lazy, optional=true) private postdetails details; ... } @entity @table(name="post_details") public class postdetails implements serializable { @id @onetoone(fetch=fetchtype.lazy, optional=false) @joincolumn(name="post_id", nullable=false) private post post; @column(name="date", nullable=false) private date date; ... }
it's basic case of one-to-one bidirectional relationship. think thing deviates basic case fact postdetails
uses post
id instead of having own id.
these tables:
this data:
and finnaly query:
postdetails pd = s.createquery( "select pd" + " postdetails pd" + " join [fetch] pd.post p" + " p.text '%banana%'" , postdetails.class) .setmaxresults(1) .getsingleresult(); system.out.println(pd.getdate()); system.out.println(pd.getpost().gettext()); // null pointer here when "join fetch" used
why pd.getpost()
gives me null
? no matter if use or not join fetch
null
. using join fetch
generated sql queries columns of both tables post
proxy still null. using eager
fetch type doesn't solve either.
if make relationship unidirectional removing postdetails
post
works fine. why that?
here complete code example:
Comments
Post a Comment