Tuesday, 15 July 2014

hibernate - @SecondaryTable Mapping from primary table (compsite primary key) to secondary table (single primary key) -



hibernate - @SecondaryTable Mapping from primary table (compsite primary key) to secondary table (single primary key) -

table :person

id (pk) first_name last_name

entity:

@entity @table(name="person") public class person { @id @column(name="id") private string id; @column(name="first_name") private string firstname; @column(name="last_name") private string lastname; @column(name="birthdate") private string birthdate; @column(name="detail") private string detail; }

table :activity

id(pk) name;

entity:

@entity @table(name="activity") public class activity { @id @column(name="id") private string id; @column(name="name") private string name; @onetomany(cascade=cascadetype.all, fetch=fetchtype.eager) @joincolumn(name="activity_id") private list<personrole> personrolelist; }

table :person_role

activity_id person_id role_name pk(activity_id,person_id)

entity:

@entity @table(name="person_role") public class personrole { @id personrolepk personrolepk; // private string personname; /* want value loaded form person table using person_id when personrole reader database */ @column(name="role_name") private string rolename; } @embeddable public class personrolepk implements serializable { public static final long serialversionuid = 1l; @column(name="activity_id") public string activityid; @column(name="person_id") public string personid; }

this working me when storing info in database

it work expected , load related personrole when want activity database

now in add-on want fill personname field of class personrole(commented) table person when personrole beingness loaded.

i read @secondarytable annotation , approached it.

but problem tutorials , documentations http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2235) show necessary have same number of primary key field (if composite key) in both of table if want map secondary table , filed mapped using @primarykeyjoincolumn

but in case there no need utilize multiple key person has single primary key

so there way map table containing composite primary key table containing single primary key utilize @secondarytable?

or,

is there other approach accomplish want.?

normally in situation have onetomany relationship in activity , person pojo's. , in personrole pojo have manytoone relationship each of activity , person, lets @ info need without duplicating of info (personname).

add onetomany in person pojo.

set<personrole> personroles = new hashset<personroles>(); @onetomany(fetch = fetchtype.eager, mappedby = "person") public set<personrole> getpersonroles() { homecoming this.personroles; } public void setpersonroles(set<personerole> personroles) { this.personroles = personroles; }

add manytoone relationship in personrole pojo.

private person person; private activity activity; @manytoone(fetch = fetchtype.eager) @joincolumn(name = "person_id") public person getperson() { homecoming this.person; } public void setperson(person person) { this.person = person; } @manytoone(fetch = fetchtype.eager) @joincolumn(name = "activity_id") public activity getactivity() { homecoming this.activity; } public void setactivity(activity activity) { this.activity = activity; }

you should able name out of person when querying on activity.

hibernate hibernate-mapping composite-primary-key

No comments:

Post a Comment