java - Hibernate: how to set relations (annotations) in a more complex model? -


i have following simplified model:

business - (1:n) - assignment - (n:1) - process 

the model classes have following annotation:

business

@lazycollection(lazycollectionoption.false) @onetomany(mappedby = "business", cascade = cascadetype.all, orphanremoval = true) private list<assignment> assignments; 

assignment

normally 1 avoid creating separate model class here, because business , process have n:m relation. need add attributes assigment itself.

@manytoone @joincolumn(name = "business_id") private business business;  @manytoone @joincolumn(name = "process_id") private process process; 

process

@lazycollection(lazycollectionoption.false) @onetomany(mappedby = "process", cascade = cascadetype.all) private list<assignment> assignments; 

requirements

  1. when business or process deleted, want assignments deleted (but not partner of relation on @onetomany side)
  2. when assignment deleted, not want remove both @onetomany sides (either business or process)

hints

  • i tried orphanremoval = true , without, got no complete sufficient solution
  • the model classes inherit mappedsuperclass provides identifier
  • the @lazycollection(lazycollectionoption.false) needed because have several @onetomany relations in business , process annotation not relate issue
  • i use h2 database , work spring's @repository interfaces when interacting persistance layer not single line of sql written

update

unfortunately thought endorsement of following annotation: @ondelete(action = ondeleteaction.cascade) , junit test approves correct working of desired behaviour hence answered question.

@test public void test() {     // stores business object in db , returns saved object     business b = createbusiness();     // stores process object in db , returns saved object     process p = createprocess();     // stores assignmnent object both relations in db , returns saved object     assignment = createassignment(b, p);     assertthat(a).isnotnull();     // deletes process object db     processservice.delete(p);     assertthat(processservice.getbyid(p.getid())).isnull();     assertthat(assignmentservice.getbyid(a.getid())).isnull();     assertthat(businessservice.getbyid(b.getid())).isnotnull(); } 

but not case. in javafx application deletion logged , looks working, when querying database afterwards, entity still in table although in junit test not... if bring light in issue thankful.

if further information needed, provide it, of course. thank in advance helping me out.

edit have solved issue , got desired behaviour following setup:

business

@lazycollection(lazycollectionoption.false) @onetomany(mappedby = "business", orphanremoval = true) @ondelete(action = ondeleteaction.cascade) private list<assignment> assignments; 

assignment

@manytoone @joincolumn(name = "business_id") private business business;  @manytoone @joincolumn(name = "process_id") private process process; 

process

@lazycollection(lazycollectionoption.false) @onetomany(mappedby = "process", orphanremoval = true) @ondelete(action = ondeleteaction.cascade) private list<assignment> assignments; 

please take note of added annotation @ondelete(action = ondeleteaction.cascade). hint came here. omitted hibernate docs here, because (imho) not provide additional useful informations feature linked post.

update: consider removed cascade attribute, not necessary because using hibernates @ondelete.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -