views:

287

answers:

1

I got several entities. Two of them got a many-to-many relation. When I do a bigger operation on these entities it fails with this exception:

org.hibernate.exception.ConstraintViolationException: could not insert collection rows:

I execute the operation i a @Transactional context. I don't do any explicit flushing i my daos. The flush is triggered by a query. In the queue are 15 elements (all of the same structure). one of them always fails (but it's always a different one (I checked) and always at a different position).

Does anybody have a hint for me for what I might do wrong?

My Mapping:

@ManyToMany(targetEntity = CategoryImpl.class)
protected Set<Category> categories = new HashSet<Category>();
A: 

Hard to say with the current level of detail. Is the association bidirectional? Do you have one owning side (with a mappedBy on the other side) defined? Can you enable logging to see the executed queries and identify the one which is causing the problem.

Pascal Thivent
The association is unidirectional. The problem occures on inserting into the join table.
Karl
@Karl Ok. Did you succeed at identifying the SQL statement that is failing?
Pascal Thivent
The query is this:Hibernate: insert into PictureImpl_CategoryImpl (PictureImpl_id, categories_id) values (?, ?)the exception the following:WARN JDBCExceptionReporter:100 - SQL Error: 1062, SQLState: 23000ERROR JDBCExceptionReporter:101 - Duplicate entry '2-18' for key 'PRIMARY'I'm using mysql (5.1.46) innodb with mysql5innodbdialect
Karl
I found a fix. I set flushmode to manual before the beginning of my operation and set it back to auto after finishing... the session was automatically flushed when i queried for something in between. this caused, but the "duplicated entry" is still weired.
Karl