java - JPA: Retrieve a list from secondary table using a composite key -
well, here have example of i'm trying no success. sorry dummy example. have following tables:
create table house( house_name varchar2(255), owner_id varchar2(255), primary key(folder_name, user_id) ); create table pet( house_name varchar2(255) not null, owner_id varchar2(255) not null, pet_name varchar2(255) not null ); alter table pet add constraint fk_pet_house foreign key(house_name, owner_id) references house(house_name, owner_id) on delete cascade; insert house values('house 1', 'owner 1'); insert house values('house 2', 'owner 1'); insert house values('house 3', 'owner 1'); insert pet values('house 1', 'owner 1', 'dog'); insert pet values('house 1', 'owner 1', 'cat'); insert pet values('house 2', 'owner 1', 'duck'); insert pet values('house 3', 'owner 1', 'bird');
as can see i'm using composite key in "house"
table. want db 3 objects this:
owner id: owner1 house name: house 1 pets: [dog, cat] owner id: owner1 house name: house 2 pets: [duck] owner id: owner1 house name: house 3 pets: [bird]
i've been searching way don't know how link these tables retrieve list. have far is:
@entity @table(name="house") @secondarytable(name="pet", pkjoincolumns={ @primarykeyjoincolumn(name="house_name", referencedcolumnname="house_name"), @primarykeyjoincolumn(name="owner_id", referencedcolumnname="owner_id"), }) public class housepets implements serializable { @id @column(name="house_name") private string housename; @id @column(name="owner_id") private string ownerid; @column(table="pet" name="pet_name") //don't know how deal private set<string> petsset = new hashset<string> //getters , setters }
and retrieve list i'm using method this:
public list<housepets> getfolderslist() { em = emf.createentitymanager(); query q = em.createquery("select h housepets h h.ownerid = :ownerid"); q.setparameter("ownerid", "owner 1"); list<housepets> result = q.getresultlist(); return result; }
thanks time!
your model makes no sense. secondarytable cannot onetomany relation.
what need owner table, defines owner_id, (maybe name, etc.)
owner have onetomany pet, , onetoone (or manytoone, onetomany?) house
pet have manytoone owner , manytoone house
house have onetomany pet , onetomany? owner
to own, select owner, pets access them.
Comments
Post a Comment