Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 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.

Author Comment

ID: 22842933

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 70
Unable to open debugger port in Intellij idea 6 322
eclipse apache tomcat admin console 52 121
jsp login check 12 42
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

789 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