This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

ConstraintViolationException: could not delete: [com.pizza.domain.Base#4]Page Title Module

ConstraintViolationException: could not delete: [com.pizza.domain.Base#4]

Jul 31st, 2010, 08:28 AM

I followed Spring Roo 1.1.0.M2 tutorial down to page 16 to produce pizzashop and perform test after adding field reference Base to Pizza. The test run ok 10 out of 10 times. However, if I change the Pizza domain to have Base all the time (ie adding @NotNull reference to Base) and perform test again. It fails about 3 or 4 out of 10 test cases. Each time the Base # is different.

adding not null on reference to other entity sometimes get ConstraintViolation

If I have more of this kind of reference on other domain, it is harder to pass the test. I have about a dozen of entity have these kind of references out of 3 dozen. I couldn't get the test to pass successfully because of "ConstraintViolationException: could not delete:"

Comment

In my opinion, this is working as it should. When the tests run, they create a Pizza that references your Base. The clue is at the bottom of com.pizza.domain.BaseIntegrationTest.txt in target/surfire-reports.

Code:

Caused by: java.sql.SQLException: Integrity constraint violation FK65BDC883CAA6D19 table: PIZZA in statement [delete from base where id=? and version=?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2694)
... 52 more

If this was a real application, you would not allow the end user to delete a base that is in use, but would instruct them to change the Bases in all existing Pizzas, and then allow them to delete the offending Base. Of course, you would write unit tests to cover that scenario.

Of course, if you just want to see that the tests all pass, modify the ManyToOne annotation like so:

Code:

@ManyToOne(targetEntity = Base.class, cascade=CascadeType.ALL)

That way the unit tests will clean up the Bases as they delete the Pizzas. However, in a real world application, this may not be what you want.