I have a @OneToMany company / employee @OneToMany in my database, which is defined as:
@Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; @ManyToOne @JoinColumn(name="companyid") Company company; .... } @Entity public class Company { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; ... }
Now I am adding a newly created employee to a separate company. The code I use looks something like this:
Company company = em1.find(Company.class, 555L); em1.close(); EntityTransaction et = em2.getTransaction(); et.begin(); Employee employee = new Employee(); employee.company = company; em2.persist(employee); et.close();
Will this work fine?
Is hibernation about to merge the company into a second EntityManager or just use your id and save the employee object?
Can hibernation somehow duplicate my company object or throw an exception, saying that a company with the same identifier already exists in the database?
java hibernate jpa entity-relationship
tal
source share