-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Importance of the saving side of a bidirectional relation
PostPosted: Thu Nov 19, 2015 9:30 am 
Newbie

Joined: Thu Nov 19, 2015 9:25 am
Posts: 2
hi

In a bidirectional relation, doe the side we do the save is important?

Code:
@Entity
public class Department {
  @OneToMany
  private Collection<employee> employees;
  ...
}


@Entity
public class Employee {
   @ManyToOne
   private Department department;
   ...
}



can we decide to do

Code:
departement.setEmployee(employees)
departmentDao.save(departement);

and

Code:
employee.setDepartment(department);
employeeDao.save(employee);


Top
 Profile  
 
 Post subject: Re: Importance of the saving side of a bidirectional relation
PostPosted: Mon Nov 23, 2015 10:02 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Two points

(1) The relationship annotations such as @javax.persistence.OneToMany, @javax.persistenece.ManyToOne supports the argument "cascase", it supports the values such as CascadeType.PERSIST, CascadeType.MERGE, etc.
When the cascade contains "CascadeType.PERSIST", The other related object(s) will be saved too automatically when the current object is saved.

(2) In bidirectional, one side must be real side and the other side must be fake side.
You can choose
Code:
public class A {
     @OneToMany(mappedBy = "a") //Fake side
     private Set<B> bs;
}
public class B {
     @ManyToOne
     @JoinColumn(...)    //Real side
     private A a;
}

or
Code:
public class A {
     @OneToMany(mappedBy = "a")
     @JoinColumn(...) //Real side
     private Set<B> bs;
}
public class B {
     @ManyToOne
     @JoinColumn(..., insertable = false, update = false) //FakeSide
     private A a;
}

,
No choice! only the changing on the real side can be use to change the relationship in database.


Top
 Profile  
 
 Post subject: Re: Importance of the saving side of a bidirectional relation
PostPosted: Thu Nov 26, 2015 2:38 pm 
Newbie

Joined: Thu Nov 19, 2015 9:25 am
Posts: 2
Ok so the save must be done on the real side. In your first example

we would need to do something like

Code:
b.setA(a);
bDao.save(b).


Top
 Profile  
 
 Post subject: Re: Importance of the saving side of a bidirectional relation
PostPosted: Sat Nov 28, 2015 1:05 pm 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Yes, you're right.

It can also be done by this way
Code:

// Change the fake side so that b should be saved when a is save(Need the cascade property of @OneToMany)
a.getBs().add(b);

//Change the real side so that the relationship of database can be changed when b is saved.
b.setA(a);

//aDAO, not bDAO
aDAO.save(b);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.