I'm having a problem with link tables and hibernate, or in other words, a many-to-many relationship problem. It is not hard to set up in hibernate; inserts and updates work fine, but the deletes fail if any of the parents share some children.
I've written an entry into my blog with more detail. It's all standard stuff, right out of the hibernate documentation. There seems to be either a gap in my understanding of how this relationship should work, or there should be another way to do it in hibernate.
I've scoured the forums looking for an answer, but have come up with nothing.
http://jwoodchip.blogspot.com/2007/05/h ... ables.html
The reason deleting an AccessLevel fails is because of the foreign keys on the link table. It always tries to delete the relationship as well as the Privilege, because of said FK's. But it lets you create these relationships (ie share Privileges among AccessLevels without problem). I've tried to prevent the delete from cascading, without success. I've tried telling the link table to take noaction when a key is deleted, without success. What I think should happen is that when you delete an AccessLevel, it should also delete the relationships for that AccessLevel, and then stop. Data integrity is always preserved this way.
So my question is, assuming this relationship is mapped properly, how do you delete an AccessLevel (without getting an error)?
Hibernate version:
3.2.3ga
Name and version of the database you are using:
Mysql 5.0.27-max
Mapping documents:
Code:
<class name="AccessLevel" table="AccessLevel">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="privileges" table="AccessLevel_Privilege">
<key column="accessLevelId"/>
<many-to-many class="Privilege" column="privilegeId"/>
</set>
</class>
<class name="Privilege" table="Privilege">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="privilegeType"/>
<property name="permissions"/>
</class>
Generated SQLCode:
create table AccessLevel (
id bigint not null auto_increment,
name varchar(255) not null,
primary key (id)) type=InnoDB;
create table AccessLevel_Privilege (
accessLevelId bigint not null,
privilegeId bigint not null,
primary key (accessLevelId, privilegeId)) type=InnoDB;
create table Privilege (
id bigint not null auto_increment,
privilegeType varchar(255) not null,
permissions smallint not null,
primary key (id),
unique (privilegeType, permissions)) type=InnoDB;
alter table AccessLevel_Privilege add index FKC1FC75729CEB5337 (accessLevelId), add constraint FKC1FC75729CEB5337 foreign key (accessLevelId) references AccessLevel (id);
alter table AccessLevel_Privilege add index FKC1FC7572EDE1B639 (privilegeId), add constraint FKC1FC7572EDE1B639 foreign key (privilegeId) references Privilege (id);
Pseudocode to reproduce
create an AccessLevel with 3 privileges and persist
create another AccessLevel with same 3 privileges and persist
delete one of the AccessLevels
Error
Foreign key constraint exception when trying to delete the AccessLevel