java - Hibernate Cascade and Foreign Key issues -
i learning hibernate cascade implement our application. application's sql server database design based on legacy db model weak/improper fk relation. , having trouble mapping entities while using hibernate cascade fetch data related tables.
consider scenario:
table_a: cola1 , cola2 makes composite key (int) (int) (string) cola1 cola2 cola3 1 1 dummy111 1 2 dummy222 2 1 dummy333 2 2 dummy444 table_b colb1 , colb2 makes composite key (int) (int) colb1 colb2 1 1 1 2 2 1 2 2 table_c colc1 , colc2 makes composite key (int) (int) (int) (str) (str) colc1 colc2 colc3 colc4 colc5 1 1 1 aa dummy999 1 2 1 bb dummy888 1 2 2 aa dummy777 colc1 , colc3 makes foreign key (cola1 , cola2 table_a when colc4=aa) or (colb1 , colb2 table_b when colc4=bb). fk not defined in table or schema written on documents , implemented across stored procedures used in application. if notice table c, colc1 , colc3 can have same values (ex, 1,1) , not unique values make fk relation. , colc4 not present in table_a or table_b.
three entities:
@entity @table(name="a") public class aentity implements serializable { @embeddedid private aentitypk id; //composite key of cola1 , cola2 //bi-directional many-to-one association centity @onetomany(mappedby="centity", cascade = {cascadetype.all}) private list<wmktempsbadwd> centitylist; } @entity @table(name="b") public class bentity implements serializable { @embeddedid private baentitypk id; //composite key of colb1 , colb2 //bi-directional many-to-one association centity @onetomany(mappedby="centity", cascade = {cascadetype.all}) private list<wmktempsbadwd> centitylist; } @entity @table(name="c") public class centity implements serializable { @embeddedid private caentitypk id; //composite key of colc1 , colc2 //bi-directional many-to-one association aentity @manytoone @joincolumns({ @joincolumn(name="cola1", referencedcolumnname="colc1", nullable=false, insertable=false, updatable=false), @joincolumn(name="cola2", referencedcolumnname="colc3", nullable=false, insertable=false, updatable=false), }) private aentity aentityobj; //bi-directional many-to-one association bentity @manytoone @joincolumns({ @joincolumn(name="colb1", referencedcolumnname="colc1", nullable=false, insertable=false, updatable=false), @joincolumn(name="colb2", referencedcolumnname="colc3", nullable=false, insertable=false, updatable=false), }) private bentity bentityobj; } sample test class fetch aentoty , related data centity: public class testfetchentitya{ public static void main(){ ..... transaction tx = session.begintransaction(); aentitylist = session.createquery("from aentity cola1=1 , cola2=1").list(); } }
executing main() fetches 2 objects centity class in centitylist aentity since haven't defined condition (colc4="aa") in mapping defined in table_c fk.
so if have add condition (colc4="aa") while mapping aentity , centity ?
note: in real application there 20-30 tables massive amount of rows mapped 5-6 tables based on scenario mentioned above.
i tried following don't work:
join between aentity , centity in createquery(), fetches values these 2 tables. have multiple entities cascaded , ignored when use join.
with @joinformula see columns can determined based on condition , not mapping itself.
Comments
Post a Comment