-->
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.  [ 1 post ] 
Author Message
 Post subject: Persisting of detached objects
PostPosted: Fri Nov 26, 2010 5:09 am 
Newbie

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


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

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.