java - How to map weak entity hibernate -
i have following relation in database:
i have 1 strong table.
i have 1 weak table has 1 one relation strong table. it's 0 1 relation, because strong table doesn't have 1 line in weak table. identify weak table enough id of strong table.
and have weak table, manytoone relation first weak entity. needs id of onetooneweakentity (that id of strong table), , own id. it's historical of onetooneweaktable.
i want map in hibernate, don't know how it.
now have following code:
@entity @table(name="table") public class table { @id @generatedvalue(strategy = generationtype.auto) @column(name="id_table") private integer idtable; private string otheratributes; .... } @entity @table(name="onetooneweaktable") public class onetooneweakentity { @onetoone(cascade = cascadetype.all, optional=false) @id @joincolumn(name="table_id_table") private table table; private string otheratributes; .... } @entity @table(name="onetomanyweaktable") @idclass(entitypk.class) public class onetomanyweaktable { @id @manytoone @joincolumn(name="table_id_table") private onetooneweakentity onetooneweakentity; @id @column(name="own_id") private string ownid; private string otheratributes; .... } class entitypk { @id @manytoone @joincolumn(name="table_id_table") private onetooneweakentity onetooneweakentity; @id @column(name="own_id") private string ownid; private string otheratributes; ....
}
my problem when try run application, because have deployment error:
caused by: org.hibernate.annotationexception: foreign key refering package.onetooneweakentity package.onetomanyweaktable has wrong number of column. should 0 @ org.hibernate.cfg.annotations.tablebinder.bindfk(tablebinder.java:502) @ org.hibernate.cfg.toonefksecondpass.dosecondpass(toonefksecondpass.java:117) @ org.hibernate.cfg.configuration.processfksecondpassinorder(configuration.java:1518) @ org.hibernate.cfg.configuration.secondpasscompile(configuration.java:1422) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1846) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1930)
how can map relation??
thanks!
edit: try map onetooneweaktable:
@onetoone(cascade = cascadetype.all, optional=false) @primarykeyjoincolumn @id @joincolumn(name="table_id_table") private table table;
in case, have error when try deploy, different exception:
caused by: java.lang.nullpointerexception @ org.hibernate.cfg.ejb3joincolumn.checkreferencedcolumnstype(ejb3joincolumn.java:568) @ org.hibernate.cfg.binderhelper.createsyntheticpropertyreference(binderhelper.java:258) @ org.hibernate.cfg.toonefksecondpass.dosecondpass(toonefksecondpass.java:116) @ org.hibernate.cfg.configuration.processfksecondpassinorder(configuration.java:1518) @ org.hibernate.cfg.configuration.secondpasscompile(configuration.java:1422) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1846)
you don't have repeat annotations in entitypk
class, need match simple primitive attribute represent compound key of onetomanyweaktable
, should (note insertable
, updatable
attributes, has no sense modify association because part of entity instance's pk),
@entity @table(name="onetomanyweaktable") @idclass(entitypk.class) public class onetomanyweaktable { @id @column(name="table_id_table") private long weakentity @manytoone @joincolumn(name="table_id_table", insertable=false, updatable=false) private onetooneweakentity onetooneweakentity; @id @column(name="own_id") private string ownid; private string otheratributes; .... } class entitypk { private long weakentity; private string ownid; .... } @entity @table(name="onetooneweaktable") public class onetooneweakentity { @onetoone @id @joincolumn(name="table_id_table") private table table; private string otheratributes; .... }
also take @ official doc of compound primary keys
edit: add onetooneweaktable
assuming using jpa 2, in edit mixing annotation. check id fileds , attribute share entities, must same type (note use long
weakentity attribute example).
Comments
Post a Comment