File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Unmanaged object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Unmanaged object " Watch "Unmanaged object " New topic

Unmanaged object

Pierre Martin

Joined: Aug 12, 2008
Posts: 4
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();

Employee emp = new Employee();


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 ?

Kumar Subramanian

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();


Subramanian, Kumar []<br />J2EE Architect, <a href="" target="_blank" rel="nofollow">Eminent Technology Solutions (ETS)</a><br />Software / Portals / Alfresco / Outsourcing / Proteomics
I agree. Here's the link:
subject: Unmanaged object