There are many similarly titled posts on here where the author is talked away from using native sql. In my case, I require a union, which, correct me if I'm wrong, isn't supported in hql or criteria, so I must go native. Now that I've done so, I need to work optimistic fetching back into my implementation and it seems like addJoin() doesn't do anything. I get no stack-traces and the result returns accurately but with lazy fetches.
Tables:
user = {user_id, user_profile_id}
user_profile = {user_profile_id, fname, lname}
Entities:
User = {userProfile}
UserProfile = {fname, lname}
Code w/ Native SQL (super-simplified):
Code:
SQLQuery query = session.createSQLQuery("" +
"select " +
" {user.*}, " +
" {userProfile.*} " +
"from " +
" user user, "+ //<-- in my actual code, "user" serves as an alias for the results of my union subsql
" user_profile userProfile "+
"where " +
" user.user_id = userProfile.user_id " +
"order by " +
" userProfile.fname, " +
" userProfile.lname " +
""
);
query.addEntity("user", User.class);
query.addJoin("userProfile", "user.userProfile");
return query.list();
Seems pretty straight-forward and this should optimistically join userProfile data to the user.userProfile attribute in the ORM, but it doesn't.
Does native sql addJoin() work?
Thanks.