java - Hibernate Composite Key Join -
i'm trying use spring data perform joined queries 1 of tables has composite key , i'm not sure how map entities.
here analogy of data model:
table: device pk=model_id pk=serial_id ... table: device_settings pk=device_settings_id fk=model_id fk=serial_id ...
here analogy of code, doesn't compile due "mappedby" attribute isn't present.
@entity @table(name = "device_settings") public class devicesettings { @id @generatedvalue(strategy = generationtype.auto) @column(name = "device_settings_id") private long id; // pretty sure problem @onetomany(targetentity = device.class, mappedby = "devicekey", cascade = {cascadetype.merge}, fetch = fetchtype.eager) @joincolumns({ @joincolumn(name = "model_id", referencedcolumnname = "model_id"), @joincolumn(name = "serial_id", referencedcolumnname = "serial_id")}) private list<device> devices; } @entity @table(name = "device") public class device { @id private devicekey devicekey; } ... } @embeddable public class devicekey implements serializable { private static final long serialversionuid = -1943684511893963184l; @column(name = "model_id") private long modelid; @column(name = "serial_id") private short serialid; }
associations marked mappedby must not define database mappings @jointable or @joincolumn
to achieve scenario have define @manytoone:
@manytoone(cascade = {cascadetype.merge}, fetch = fetchtype.eager) @joincolumns({ @joincolumn(name = "model_id", referencedcolumnname = "model_id"), @joincolumn(name = "serial_id", referencedcolumnname = "serial_id")}) private device device;
this end model_id, serial_id, device_settings_id
or
define @joincolumn in device entity entities:
devicesettings :
@entity @table(name = "device_settings") public class devicesettings { @id @generatedvalue(strategy = generationtype.auto) @column(name = "device_settings_id") private long id; @onetomany( mappedby = "devicesettings", cascade = {cascadetype.merge}, fetch = fetchtype.eager) private list<device> devices; }
device entity :
@entity @table(name = "device") public class device { @embeddedid private devicekey devicekey; @manytoone @joincolumn(name="device_settings_id") private devicesettings devicesettings; //getters , setters }
note : can decide owner of relationship , put mappings accorindly either 1 device has many device settings or other way around.
Comments
Post a Comment