Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Unmanaged references

You should not reference your own stub object in your persistence context. 
Even if successful this will corrupt your persistence context and if caching
possibly your persistence unit.  Instead use find() to find the object, or
use getReference() to get a managed stub.



mgsoft wrote:
> 
> In our project we are working with data transfer objects. In boundary
> methods a conversion from DTO to respective entity takes place.
> 
> public void updateCustomer(CustomerUpdateDTO dto) 
> {
>   // Find and attach customer entity
>   Customer customer = crudService.findCustomerByCode(dto.getCode());
> 
>   customer.setName(dto.getName());
>   // Rest omitted
> 
>   // The customer DTO references a list DTO for the customer's country
>   // Of course the respective country entity exists in the database. 
>   customer.setCountry(new Country(dto.getCountry().getCode()));
> }
> 
> When flushing the entity manager tries to persist the "non-existing"
> country. That's interesting because the association isn't annotated with a
> cascade option! 
> Anyway, the operation fails:
> com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:
> Column 'name' cannot be null
> Error Code: 1048
> Call: INSERT INTO country_tab (code, name) VALUES (?, ?)
> 
> The property "eclipselink.validate-existence" seems to work only when
> persisting new objects.
> 
> 
> 
> 
> James Sutherland wrote:
>> 
>> Although not generally a good idea, JPA does allow you to persist an
>> object with a reference to a non-managed existing object.  Is the object
>> existing?  What error do you get?
>> 
>> In general if you use merge() instead of persist() it should ensure the
>> correct managed object is referenced.  You could also manually lookup the
>> correct managed object to be referenced.
>> 
>> You could try the property "eclipselink.validate-existence", it may
>> change the behavior.
>> 
>> 
>> 
>> mgsoft wrote:
>>> 
>>> Hi,
>>> 
>>> we are just in transition from Hibernate to EclipseLink. While running
>>> the first tests we saw a difference between Hibernate and EclipseLink:
>>> 
>>> If I want to persist or update an entity that references an unmanaged
>>> object I get an error message that tells me that persisting the
>>> referenced unmanaged entity failed. Of course I don't want to cascade
>>> e.g. the persist of the entity reference that even hasn't an apporpriate
>>> annotation (Cascade.ALL or Cascade.Persist).
>>> In Hibernate it works without a problem. Do I have to attach (e.g by
>>> using find) the reference always before persisting an entity or is there
>>> a global setting to change this behaviour?
>>> 
>>> Thanks in advance!
>>> 
>>> 
>>> 
>> 
>> 
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/Unmanaged-references-tp31039202p31108309.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top