Joined: Fri Nov 26, 2010 4:38 am Posts: 1
|
I have a company/employee relation in my database 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 an adding a newly created employee to a detached company. The code I use is something like:
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 ok? Is hibernate going to merge the company into the 2nd EntityManager or just use its id and persist the employee object? Might hibernate somehow duplicate my company object or throw an exception saying that a company with the same id already exists in the DB?
Thanks.
Tal
|
|