users@glassfish.java.net

Using Existing Relations

From: <glassfish_at_javadesktop.org>
Date: Thu, 21 Feb 2008 05:10:43 PST

Hello

I suppose this is a beginner's question but I have been unable to find the answer on the Internet:

I want to create a new object and related it to already existing objects.

e.g:
Car car = em.createNamedQuery("Car.findById", 1);
Garage garage = new Garage();
garage.setCar(car);
em.persist(garage);

When run, I get the following exception:

Exception in thread "main" javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object

I understand that the reason here is that I am trying to persist an object that already exists (car).

How do you bypass this?

Thanks in advance.
[Message sent by forum member 'entername' (entername)]

http://forums.java.net/jive/thread.jspa?messageID=260174