tags:

views:

35

answers:

1

I have a grails project that is throwing the following exception:

org.springframework.dao.DataIntegrityViolationException: could not delete: [Role#4]; SQL [delete from role where id=? an
d version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not del
ete: [Role#4]

In my Role domain, all I did to create this error, was change the definition of one of the variables from

 List<RoleTool> roleTools = new ArrayList<RoleTool>()

to

ArrayList<RoleTool> roleTools = new ArrayList<RoleTool>()

Why is that?

+1  A: 

It's bad practice in general to specify a concrete class as the declaration type, both in variable declarations and in method signatures. Unless you really need it to be an ArrayList, leave it as List to allow more flexibility.

I'm not entirely sure what's happening here, but Hibernate has its own collections classes that it uses for mapped collections, the most commonly used being org.hibernate.collection.PersistentList and org.hibernate.collection.PersistentSet. These implement the List and Set interfaces respectively, but do not extend ArrayList or HashSet or any typical concrete collection. Instead they're Hibernate internal classes that monitor changes to help with dirty detection when persisting, flushing, etc.

It's fine to declare the initial collection as an ArrayList since it's only read from when saving (it's Groovy though, so it's a lot cleaner to just use List<RoleTool> roleTools = []). But Hibernate needs the flexibility of implementing the List/Set interface when loading persistent instances.

Burt Beckwith
This question was actually a follow-on to a previous question I had posted about doing custom XmlAdapter for a List in a grails project. Can you make a recommendation for how I would work that? http://stackoverflow.com/questions/3962209/how-to-use-generic-object-list-in-jaxb-xmladapter
Derek
We are working on a fix in MOXy JAXB that would let you use List instead of ArrayList for this case. You can track the issue with the following bug: https://bugs.eclipse.org/328079
Blaise Doughan