Solved

How to persist an unmanaged property object

Posted on 2008-10-20
3
261 Views
Last Modified: 2013-11-24
Hi,
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();
dep.setId(44);

Employee emp = new Employee();
emp.setDepartement(dep);

entityManager.persist(emp);

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 ?

Thanks!



0
Comment
Question by:kmapper
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
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

    em.persist(emp);

    em.flush();

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

    query.executeUpdate();

}

Open in new window

0
 
LVL 4

Expert Comment

by:Mr_It
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.
 
0
 

Author Comment

by:kmapper
ID: 22842933
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now