| Author |
Unmanaged object
|
Pierre Martin
Greenhorn
Joined: Aug 12, 2008
Posts: 4
|
|
Hi, I'm using EJB 3.0 entity bean. I want to save a new Employee entity bean and associate it to the department id = 44; To way it works, I need to 1-fetch the department entity bean 2-Set the department into the employee 3-Save the employee. If you notices, the first step is useless since I already know the Id of the department. Why should I fetch it and get a manageg entity bean? I would expect to be able to do that kind of save: Department dep = new Department(); dep.setId(44); Employee emp = new Employee(); emp.setDepartement(dep); entityManager.persist(emp); But that doesnt work because the framework forces to have a managed object as the department. I would have though that when I set CascadeType to nothing it would do the trick but it doesnt. Is there any workaround for this ? Tanx!
|
 |
Kumar Subramanian
Greenhorn
Joined: Oct 01, 2008
Posts: 8
|
|
Use the getReference() method on entity manager. It will return the reference to your Department entity (just a lazy loadable proxy) for the pk passed as parameter. Use that reference to set in your employee object. This will not do a db select in case of hibernate JPA impl and I think it should be the same in other implementations. Department dep = entityManager.getReference(Department.class, 44); Employee emp = new Employee(); emp.setDepartement(dep); entityManager.persist(emp);
|
Subramanian, Kumar [kumar@eminenttech.com]<br />J2EE Architect, <a href="http://www.eminenttech.com" target="_blank" rel="nofollow">Eminent Technology Solutions (ETS)</a><br />Software / Portals / Alfresco / Outsourcing / Proteomics
|
 |
 |
|
|
subject: Unmanaged object
|
|
|