The problem is generic (not specific to these classes). I've probably misunderstood the way collections work but I haven't been able to sort it out via google/hibernate.org/java persistance with hibernate.
I have 2 classes:
Code:
public class MemoryBO extends AbstractBO {
protected int userId;
protected String title;
protected String contents;
protected Collection<TagBO> tags;
public MemoryBO() {}
... getters/setters removed for brevity...
}
public class TagBO extends AbstractBO {
protected String tag;
public TagBO() {}
... getters/setters removed for brevity...
}
This is mapped as such:
Code:
<class name="bo.MemoryBO" table="Memories">
<id name="id">
<generator class="native"/>
</id>
<property name="userId"/>
<property name="title"/>
<property name="contents"/>
<set name="tags" cascade="save-update">
<key column="parent_id"/>
<one-to-many class="bo.TagBO" />
</set>
</class>
<class name="bo.TagBO" table="Tags">
<id name="id">
<generator class="native"/>
</id>
<property name="tag"/>
<property name="created" type="timestamp"/>
</class>
The table definitions are thus:
Code:
CREATE TABLE Memories (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
userId INT NOT NULL,
CONSTRAINT FOREIGN KEY (userId) REFERENCES Users(id),
title VARCHAR(80) NOT NULL,
contents TEXT NOT NULL
) TYPE=InnoDB;
CREATE TABLE Tags (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
tag VARCHAR(20) NOT NULL,
memoryId INT NOT NULL,
CONSTRAINT FOREIGN KEY (memoryId) REFERENCES Memories(id),
Created TIMESTAMP(14)
) TYPE=InnoDB;
I can load the data fine. When I attempt to save an instance of MemoryBO with some tags in it, the insert fails due to a null constraint - because hibernate isn't including the id of the memoryBO object in the insert to Tags(memoryId).
What I haven't been able to figure out is how I tell hibernate to set that field to the ID of the container class. I've tried this with a "int memoryId" field in TagBO but still can't figure out how to make hibernate set the value correctly.
Any help is greatly appreciated.