java - Use id and version columns to map one-to-many relationship -


i have 2 jpa entities, parent , child.

parent has @id , @version.

for each parent (id , version fks) there multiple children.

@entity @table(name = "parent") public class parentdao implements serializable {      @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "parent_id")     private long parentid;      @version     @column(name = "parent_version", updatable = false)     private long parentversion;  ... } 

and

@entity @table(name = "child") public class childdao implements serializable {      @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "child_id")     private long childid;      @column(name = "parent_id", updatable = false)     private long parentid;      @column(name = "parent_id", updatable = false)     private long parentversion;  ... } 

if version not have constraint, fk in child towards parent, relation have been.

@onetomany(mappedby = "parentid", cascade = cascadetype.all) private set<childdao> children = new hashset<>(0); 

hibernate treats version special field since there @version annotation set @ attribute level.

so this, can imagine maybe thought of when implementing one-to-many relation.

nonetheless, there might more generic solution.

so, how can add version constraint in 1 many relation?

in rdbms, fk references pk. so, child fk must reference parent entity identifier, right?

the version should never form fk because version keeps on changing, while fk should not since must reference immutable fk.

if need custom way of expressing collection, should use @joinformla instead of @joincolumn.

more, child mapping wrong anyway since duplicating parent_id mapping:

@column(name = "parent_id", updatable = false) private long parentid;  @column(name = "parent_id", updatable = false) private long parentversion; 

maybe wanted have:

@column(name = "parent_id", updatable = false) private long parentid;  @column(name = "parent_version", updatable = false) private long parentversion; 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -