Ognjen Blagojevic wrote:
> 
> It is, indeed. Thanks.
> 
> Now I really understand why did the specification declared both "find" 
> and "getReference". O:)
> 
> Regards,
> Ognjen
> 
> 
> Patrick Linskey wrote:
>> Hi,
>> 
>> EntityManager.getReference() is your friend here.
>> 
>> -Patrick
>> 
>> On Thu, Apr 3, 2008 at 4:19 AM, Ognjen Blagojevic <[EMAIL PROTECTED]>
>> wrote:
>>> Hi all,
>>>
>>>  Is there a way to persist an Entity without loading all the referenced
>>> objects?
>>>
>>>  I know that this is possible:
>>>
>>>  Employee e = new Employe();
>>>  e.setName("John Doe");
>>>  e.setIdDepartment(findDepartmentByPrimaryKey(idDept));
>>>  e.setIdRank(findRankByPrimaryKey(idRank));
>>>  // and so on
>>>  em.persist(e);
>>>
>>>  But if I have a lot of references, and lot of INSERTS to do, this
>>> becames
>>> import with poor performace (before each INSERT statemet app must
>>> execute N
>>> selects).
>>>
>>>  It would be much faster if I can set references without looking them
>>> up:
>>>
>>>  Employee e = new Employe();
>>>  e.setName("John Doe");
>>>  Department dept = new Department();
>>>  dept.setIdDepartment(idDept);
>>>  e.setIdDepartment(dept);
>>>  // and so on
>>>  em.persist(e);
>>>
>>>  (Please note here: I'm not trying to insert new department, but rather
>>> to
>>> set a reference to existing one without looking it up in the database.)
>>>
>>>  But this seems impossible. Without CascadeType.PERSIST it throws
>>> something
>>> like:
>>>
>>>  <openjpa-1.0.2-r420667:627158 nonfatal user error>
>>> org.apache.openjpa.persistence.InvalidStateException: Encountered
>>> unmanaged
>>> object "[EMAIL PROTECTED]" in persistent field
>>> "mypackage.Employee.idDepartment" of managed object
>>> "mypackage.Employee-5"
>>> during flush.  However, this field does not allow to be
>>> CascadeType.PERSIST.
>>> You cannot flush unmanaged objects.
>>>
>>>  Ok, I add the CascadeType.PERSIST, and then:
>>>
>>>  org.apache.openjpa.persistence.InvalidStateException: The generated
>>> value
>>> processing detected an existing value assigned to this field:
>>> mypackage.Department.idDepartment.  This existing value was either
>>> provided
>>> via an initializer or by calling the setter method.  You either need to
>>> remove the @GeneratedValue annotation or modify the code to remove the
>>> initializer processing.
>>>
>>>
>>>  It must be a way to INSERT objects in the database without looking up
>>> for
>>> all the references, but I am not able to find it.
>>>
>>>  Regards,
>>>  Ognjen
>>>
>> 
>> 
>> 
> 
> 
> 

I've been facing the same kind of problem ; my program keeps complaining
about "unmanaged object" despite using EntityManager.getReference() as
suggested by Patrick. I wonder what I've been doing wrong. Would you show
the piece of code (and possibly the related configuration part in orm.xml)
that made your program run ?

regards !

Edmond/

-- 
View this message in context: 
http://n2.nabble.com/How-to-PERSIST-object-without-loading-references--tp210631p735154.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to