How to persist an unmanaged property object

Posted on 2008-10-20
Medium Priority
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 2000 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 e.department.id = :depId WHERE e.id = :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.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

588 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