[hibernate-users] All OneToMany references being deleted upon update

Robert Campbell rrc7cz at gmail.com
Mon Jun 28 15:54:28 EDT 2010


I've been using JPA w/Hibernate 3.4.0.GA for a long time now, and I
never experienced this behavior before:

I have a couple OneToMany relationships in a couple Embeddable
classes. When I update any of these relationships, either by removing
a member or even changing a field in the member, randomly the entire
cross reference table is cleared for that entire Entity.

Let me be more specific:

I have a "User" Entity with a "Foo" Embeddable reference. This "Foo"
class also has a "Bar" Embeddable reference. Foo has a
OneToMany(Cascade.ALL) Set<Baz> and Bar has two OneToMany(Cascade.ALL
minus DELETE) Set<Baz>. Hibernate has created a "user_baz" cross
reference table to store the user_id x baz_id relationship.

I have a Transactional method which loads the User via the
EntityManager, then performs some update on one of the Baz objects,
either updated a field or removing it from the collection. It actually
looks like this:

	@Transactional
	public void removeBaz(final Long userId, final Long bazId) {
		final User user = getUserById(userId);
		user.removeBaz(bazId);		
	}

Mostly this works as desired, with the Baz in question being marked as
deleted or having the reference actually deleted (I've played
w/different versions). Sometimes, however, the entire "user_baz" cross
reference table for that user will get wiped out. Even if I have 20
references in each of the three Sets, all of them just disappear from
the table. The actual Baz records in the baz table remain.

I've traced the code line by line, and the user code (stuff I wrote)
is all functioning properly. In other words, all three collections
(and their contents) appear correct after "user.removeBaz(bazId);"
returns. Clearly something is wrong.

Any ideas what the problem might be? Any hints for debugging this?

Rob


More information about the hibernate-users mailing list