Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] EntitEntityManager.remove: Why does it care about nullable=false?

The update occurs because in many cases an object must be updated before
being deleted, such as for constraints between objects.  If you do not want
the object updated, the best solution is to not modify it.  You could
potentially also refresh or detach (unregister and reregister) the object
before calling remove to clear any changes made to it.  There is also an
EclipseLink option shouldPerformDeletesFirst that can avoid this, but in
general, do not update an object that you do not want updated.



Francisco P. wrote:
> 
> We have an entity like this:
> 
> @Entity
> public class Customer {
> 
> 	
> 	private Long id;
> 	private String name;
> 	
> 
> 	@Id
> 	@GeneratedValue(strategy=GenerationType.AUTO)
> 	public Long getId() {
> 		return id;
> 	}
> 
> 	public void setId(Long id) {
> 		this.id = id;
> 	}
> 	
> 	@Column(nullable=false)
> 	public String getName() {
> 		return name;
> 	}
> 
> 	public void setName(String name) {
> 		this.name = name;
> 	}
> }
> 
> 
> and them we have the following unit-test: 
> 
> 	@Test
> 	public void persistAndRemoveCustomer() throws Exception {
> 		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
> 		EntityManager em = factory.createEntityManager();
> 
> 		em.getTransaction().begin();
> 
> 		Customer customerToPersist = new Customer();
> 		customerToPersist.setName("Joseph");
> 		em.persist(customerToPersist);			
> 		
> 		em.getTransaction().commit();
> 							
> 		em.close();
> 		
> 		em = factory.createEntityManager();
> 		em.getTransaction().begin();
> 		Customer foundCustomer = em.find(Customer.class,
> customerToPersist.getId());
> 		Assert.assertNotSame(customerToPersist, foundCustomer); //Not same
> instance, different entityManager
> 		foundCustomer.setName(null);
> 		em.remove(foundCustomer);
> 		em.getTransaction().commit();
> 		
> 
> 	}
> 
> 
> Does it crash or does it work perfecty? Well, it turns out it crashes on
> line em.getTransaction().commit(); saying:
> 
> avax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse
> Persistence Services - 1.1.0.r3634):
> org.eclipse.persistence.exceptions.DatabaseException
> Internal Exception: java.sql.SQLException: Attempt to insert null into a
> non-nullable column: column: NAME table: CUSTOMER in statement [UPDATE
> CUSTOMER SET NAME = ? WHERE (ID = ?)]
> 
> That is because I set the name of the customer to null in the line
> "foundCustomer.setName(null);" but... why does EclipseLink worry about
> that? why does it waste time updating if the final result is going to be
> the deletion of the object (and delete does not care if name is null)?
> 
> 
> 


-----
---
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 
-- 
View this message in context: http://www.nabble.com/EntitEntityManager.remove%3A-Why-does-it-care-about-nullable%3Dfalse--tp22942681p22950829.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top