java - Hibernate : @OneToOne gives null value -
i'm trying map onetoone relationship entity identifier : in schema person can have 0 or 1 user account, user_accounts table's id foreign key referencing person's id.
here's sample of class diagram :
i have used joined inheritance strategy.
schema :
- persons (id, ..., pk(id))
- customers (id, ..., pk(id), fk(id) rf persons(id))
- user_accounts(id, ..., pk(id), fk(id) rf persons(id))
here's sample of useraccount class :
@entity @table(name = "user_accounts") public class useraccount extends core.business.model.mapping.entity implements serializable { @id @onetoone(targetentity = person.class, cascade = cascadetype.all) @joincolumn(name = "id", referencedcolumnname = "id", nullable = false) private registereduser user; @column(name = "email_address") private string emailaddress; @column private string hash; @onetoone(mappedby = "useraccount", cascade = cascadetype.all, fetch = fetchtype.lazy) private token token; @override public boolean equals(object o) { if (this == o) return true; if (o == null || getclass() != o.getclass()) return false; useraccount = (useraccount) o; return user.equals(that.user); } @override public int hashcode() { return user.hashcode(); } useraccount() { } }
after useraccount returned dao, user null, cannot possible according model's constraints.
here's sql query generated hibernate :
select useraccoun0_.id id4_25_, useraccoun0_.updated_at updated_1_25_, useraccoun0_.email_address email_ad2_25_, useraccoun0_.hash hash3_25_, useraccoun0_1_.token_id token_id0_24_ user_accounts useraccoun0_ left outer join tokens__user_accounts useraccoun0_1_ on useraccoun0_.id=useraccoun0_1_.user_account_id useraccoun0_.email_address='xxx@xxx.com' , useraccoun0_.hash='e13d212084f7d594e4c540474c0981d99dfe581a'
there no join on persons table while expected fetch it.
if move registreduser @embeddable class , embed @embeddedid, relationship fetched, it's quite "ugly" proceed , hibernate specs @ the end of 2.2.3.1 section :
finally, can ask hibernate copy identifier associated entity. in hibernate jargon, known foreign generator jpa mapping reads better , encouraged
Comments
Post a Comment