java - Many-to-Many Relationship in Hibernate with Extra Column -


i have database stucture this. user access control in application.

 usergroup  ==========  id (pk)  code  name    hakakses  ==========  id(pk)  usergroup_id (fk)  akses_id (fk)   action_create  action_read  action_update  action_delete    akses  ==========  id(pk)  code 

and make hibernate annotation this

@entity @table(name = "[master].[usergroup]") public class usergroup implements serializable{     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @column(unique = true, length = 10)     private string kode;     private string nama;      @onetomany(mappedby = "usergroup")     private set<hakakses> hakaksesset; }  @entity @table(name = "[master].[hakakses]") public class hakakses implements serializable {     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @manytoone     @joincolumn(name = "usergroup_id", nullable = false)     private usergroup usergroup;      @manytoone     @joincolumn(name = "akses_id", nullable = false)     private akses akses;      @column(name = "action_create")     private boolean cancreate;     @column(name = "action_read")     private boolean canread;     @column(name = "action_update")     private boolean canupdate;     @column(name = "action_delete")     private boolean candelete; }  @entity @table(name = "[master].[akses]") public class akses implements serializable {     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @column(unique = true)     private string kode;      @onetomany(fetch = fetchtype.lazy, mappedby = "akses")     private list<hakakses> hakaksesset; } 

and tried in tester code

usergroup usergroup = new usergroup(kode, nama); usergroup.sethakaksesset(new hashset<hakakses>(hakakseslist)); this.controller.save(usergroup); 

but when run tester, data saved in database usergroup data. there problem hibernate annotation?

thank you

your mapping uses mappedby, among other settings, inject inverse="true" setting one-to-many end. read more here:

in nutshell, means, hibernate instructed: other (many-to-one) end care persisting. also, try go through example in documentation

23.2. bidirectional one-to-many

this example shows inverse in action, essential cites:

... child entity managing state of link, tell collection not update link. use inverse attribute this:

<set name="children" inverse="true">     <key column="parent_id"/>     <one-to-many class="child"/> </set> 

the following code used add new child:

parent p = (parent) session.load(parent.class, pid); child c = new child(); c.setparent(p); p.getchildren().add(c); session.save(c); session.flush(); 

only 1 sql insert issued.

so see there?

  • inverse setting need. improves performance (and have due mappedby)
  • but not enough set relation on one-to-many side. must set parent each child.

so, should extend this:

usergroup usergroup = new usergroup(kode, nama); usergroup.sethakaksesset(new hashset<hakakses>(hakakseslist)); // not enough - must  // each hakakses in  list  // set parent, i.e hakaksses.setusergroup(usergroup) 

finally, there (almost) should cascading turned on. 1 should not care manually:

@onetomany(mappedby = "usergroup", cascade = cascadetype.all) 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -