java - JPA OneToOne Lazy relation -
i have 2 entities: user , userprofile have bidirectional @onetoone relationship between them.
due old db design userprofile owner (i have column user_id in users_profiles table)
relationship lazy have fetchtype lazy , optional = false.
works expected, mean when load userprofile not automatically loads user also. guess normal load owner side.
problem if load user (owned side) loads automatically userprofile although relationship lazy.
i mean: normal when load entity owned side load owner entity ?
@entity @table(name = "users") public class user extends baseentity implements serializable { @onetoone(mappedby = "user", optional=false, fetch = fetchtype.lazy) private userprofile profile; // .................rest of entity } @entity @table(name="users_profiles") public class userprofile extends baseentity implements serializable { @onetoone(optional=false, fetch = fetchtype.lazy) @joincolumn(name="user_id") private user user; // ... rest of entity here }
the way test loading user entity entitymanager method find(id).
have noticed when relation not lazy have 1 query join inside. if put current setup have 2 individual queries: 1 user , other 1 profile.
it important realize, lazy loading behavior not enforced jpa specification, recommended. hibernate implementation choose if supported , under conditions.
hibernate best load data lazily when requested, in case of one-to-one
mapping, when relationship stored in table (the primary key in table users_profiles
), needs query second table retrieve primary key create proxy object. in fact, not retrieve id, full row , creates userprofile
in eager way, because costs nothing fetch additinal data when table needs joined in case.
the answer hibernate: one-to-one lazy loading, optional = false suggests making relationship non-optional should make lazy, doubt true. hibernate need create proxy without id, doubtfully correct in cases. 1 case fail when remove proxy object collection in parent entity, , try read data - proxy not carry enough information retrieve data lazily, not possible without connection parent entity.
Comments
Post a Comment