java - Hibernate - how to use cascade in relations correctly -
i'm using hibernate in spring mvc application , have question cascade. see many similar questions it, none of them can answer question. suppose have user
, userposition
objects. user
has collection of userposition
, has 1 userposition
default position. structure this:
user:
@onetomany(mappedby = "user", fetch = fetchtype.lazy) private collection<userposition> userpositioncollection; public collection<userposition> getuserpositioncollection() { return userpositioncollection; } public void setuserpositioncollection(collection<userposition> collection) { this.userpositioncollection = collection; } @onetoone(fetch = fetchtype.lazy, cascade = cascadetype.all) @joincolumn(name = "default_user_position_id", referencedcolumnname = "id") private userposition defaultuserposition; public userposition getdefaultuserposition() { return defaultuserposition; } public void setdefaultuserposition(userposition defaultuserposition) { this.defaultuserposition = defaultuserposition; }
userposition:
@joincolumn(name = "user_id", referencedcolumnname = "id") @manytoone(fetch = fetchtype.lazy, cascade = cascadetype.persist) private user user; public user getuser() { return user; } public void setuser(user user) { this.user = user; } @onetoone(fetch = fetchtype.lazy, mappedby = "defaultuserposition", cascade = cascadetype.persist) private user defaultuserposition; public user getdefaultuserposition() { return defaultuserposition; } public void setdefaultuserposition(user defaultuserposition) { this.defaultuserposition = defaultuserposition; }
now, question what's best practice use cascade saving related objects?. in fact, confused these 3 solutions:
solution 1:
user user = new user(); //some setters , getters userposition userposition = new userposition(); //some setters , getters list<userposition> positionlist = new arraylist<>(); positionlist.add(userposition); user.setdefaultuserposition(userposition); user.setuserpositioncollection((collection<userposition>) positionlist ); session.persist(user)
solution 2:
user user = new user(); //some setters , getters userposition userposition = new userposition(); //some setters , getters userposition.setuser(user); userposition.setdefaultuserposition(user); session.persist(userposition)
solution 3(combination of 2 previous solutions):
user user = new user(); //some setters , getters userposition userposition = new userposition(); //some setters , getters list<userposition> positionlist = new arraylist<>(); positionlist.add(userposition); user.setdefaultuserposition(userposition); user.setuserpositioncollection((collection<userposition>) positionlist ); userposition.setuser(user); userposition.setdefaultuserposition(user); session.persist(user);
this important me, please me. solution correct , should cascade
property? thank time.
the user
parent entity , cascading always propagates parent child entities.
therefore, user associations become:
@onetomany(mappedby = "user", fetch = fetchtype.lazy, cascade = cascadetype.all, orphanremoval = true) private collection<userposition> userpositioncollection;
but default position, user becomes child
of association:
@onetoone(fetch = fetchtype.lazy) @joincolumn(name = "default_user_position_id", referencedcolumnname = "id") private userposition defaultuserposition;
in userposition
class other way around:
@joincolumn(name = "user_id", referencedcolumnname = "id") @manytoone(fetch = fetchtype.lazy) private user user;
and
@onetoone(fetch = fetchtype.lazy, mappedby = "defaultuserposition", cascade = cascadetype.persist) private user defaultuserposition;
then have add following utility methods synchronize both sides. these go user
class:
public void adduserposition(userposition userposition) { userpositioncollection.add(userposition); userposition.setuser(this); } public void adddefaultuserposition(userposition userposition) { defaultuserposition = userposition; userposition.setdefaultuserposition(this); }
the persisting logic becomes:
user user = new user(); //some setters , getters userposition userposition = new userposition(); //some setters , getters user.adduserposition(userposition); user.setdefaultuserposition(userposition); session.persist(user);
Comments
Post a Comment