java - Can I map a relationship inJPA with a unique target that is not the id? -
i'd able use dot notation 1 entity another.
let's have table users:
id username acronym ----------------------- 1 john nasa and table institutions:
id instname acronym --------------------------- 9 natl. air... nasa for historical reasons, users table not contain reference institution id, acronym unique.
is there possibility dot way entity john name of nasa in john.getinstitution().instname()? i'd avoid having run query through entity manager.
have tried it?
as far can see then, depending on jpa provider, following mappings may or not supported.
@entity public class institution{ @onetomany(mappedby = "institution") private set<user> users; } @entity public class user { @manytoone @joincolumn(name = "acronym", referencedcolumname = "acronym") private institution institution; } failing this, other possibility create database view based on following , map user entity this.
select u.id, u.username, i.id institution_id users u inner join institutions on u.acronym = i.acronym entity 'proper' fk mapping:
@entity @table(name = "vw_users") public class user { @manytoone @joincolumn(name = "institution_id") private institution institution; }
Comments
Post a Comment