hibernate - Redundant column - one to one mapping with composite id -
this model
@entity public class picture { @embeddedid private pictureid id; ... } @embeddable public class pictureid implements serializable { private static final long serialversionuid = -8285116986358642545l; @column(nullable = false, name = "gallery_id") private long galleryid; @column(nullable = false, name = "author_id") private long authorid; @column(nullable = false, name = "ordinal_number") private int ordinalnumber; ... } @entity public class gallery { @id @generatedvalue private long id; private string name; @onetoone @joincolumns({ @joincolumn(name = "redundant_gallery_id", referencedcolumnname = "gallery_id"), @joincolumn(name = "cover_author_id", referencedcolumnname = "author_id"), @joincolumn(name = "cover_ordinal_number", referencedcolumnname = "ordinal_number") }) private picture cover; ... }
i guess specifing @joincolums
here not necessary wanted make more clear. model there redundant column created in gallery
table mirrors value of id
. rid of column. field cover
might null.
after thinking lot on decided better move mapping external table. didn't solve problem , gets more complicated.
@entity public class gallery { @onetoone @jointable(name = "gallery_cover", joincolumns = { @joincolumn(name = "gallery_id", referencedcolumnname = "id") }, inversejoincolumns = { @joincolumn(name = "redundant_gallery_id", referencedcolumnname = "gallery_id"), @joincolumn(name = "author_id", referencedcolumnname = "author_id"), @joincolumn(name = "ordinal_number", referencedcolumnname = "ordinal_number") }) private picture cover; }
i trying use @associationoverride
, @attributeoverride
failed. can me these mappings?
edit: here repository model
forget join table , redundant column name. it's ok use same column name twice in 1 entity long 1 instance of can inserted/updated. @column
, @joincolumn
annotations let override default insertable/updatable values.
@entity public class gallery { @id @generatedvalue private long id; private string name; @onetoone @joincolumns({ // no new column name here, specify insertable/updatable @joincolumn(name = "gallery_id", insertable=false, updatable=false), @joincolumn(name = "cover_author_id", referencedcolumnname = "author_id", insertable=false, updatable=false), @joincolumn(name = "cover_ordinal_number", referencedcolumnname = "ordinal_number", insertable=false, updatable=false) }) private picture cover; ... }
the cleaner approach, however, change primary key on picture
doesn't include galleryid
. if authorid
, ordinalnumber
specify unique key, remove galleryid
join.
Comments
Post a Comment