I'm trying to set up a many-to-one relationship where I have one User and multiple Sailor instances. If I delete a User, all Sailors with matching userId's should be deleted.
I don't want to have a <one-to-many> relationship in the User class, as I don't want to populate the User object with lots of Sailor objects every time I fetch the former from the database.
I'm using the mapping and DLL below. What happens is the opposite of what I want: when a Sailor is deleted, the associated User is deleted as well.
Any hints?
Hibernate version:
3.2.3
Mapping documents:
Code:
<hibernate-mapping>
<class name="jotto.User" table="User">
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="email" />
<property name="password" />
<property name="name" />
</class>
<class name="jotto.Sailor"
table="Sailor">
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="userId" />
<property name="cash" />
<many-to-one name="user" column="userId"
class="jotto.User"
lazy="false" not-found="ignore"
insert="false" update="false" cascade="delete">
</many-to-one>
</class>
</hibernate-mapping>
The DDL (mySQL)Code:
CREATE TABLE User
(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
name VARCHAR(255),
password VARCHAR(255),
CONSTRAINT UNIQUE(email)
) ENGINE=InnoDb;
CREATE TABLE Sailor
(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
userId INT NOT NULL,
cash BIGINT,
CONSTRAINT UNIQUE(userId),
FOREIGN KEY (userId) REFERENCES User (id)
) ENGINE=InnoDb;