Skip to main content

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

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)?


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



Back to the top