How to persist an unmanaged property object

Posted on 2008-10-20
Last Modified: 2013-11-24
I'm using an EJB 3.0 entity bean.

I want to save a new Employee entity bean and associate it to the department id = 44;

The 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 notice, the first step is useless since I already know the Id of the department. Why should I fetch it and get a managed entity bean?

I would expect to be able to do that kind of save:

Department dep = new Department();

Employee emp = new Employee();


But that doesn't work because the framework forces to have a managed object as the department. I would have thought that when I set CascadeType to nothing it would do the trick but it doesnt.

Is there any workaround for this ?


Question by:kmapper
  • 2

Accepted Solution

Mr_It earned 500 total points
ID: 22831262
If you don't already have or need the object, don't load it in memory. Obviously you don't need the Department object, so you are absolutely right that you don't want to load it.

Doesn't something like the code below work?
public void createEmployee(Employee emp, String departmentID) {
    emp.setDepartment(null); // If set to CascadeType.PERSIST, be sure NOT to have an unmanaged department in emp
    Query query = em.createQuery("UPDATE Employee e SET = :depId WHERE = :empId");
    query.setParameter("depId", departmentID);
    query.setParameter("empId", emp.getId());

Open in new window


Expert Comment

ID: 22842180
Thanks :-) I know it's still an INSERT and UPDATE statement, while it could be done in 1 INSERT statement if you use native SQL... This is just one disadvantage of using an ORM like JPA. It does not always do the most performant things.

Keep in mind that your managed Employee entity is not in sync anymore after such and UPDATE (or DELETE) statement. Maybe I should have put an em.clear() call as well after the flush.

Author Comment

ID: 22842933

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 70
custom annotations 9 38
diffSum example 4 36
servlet filter example 37 59
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question