I seemed to have this working for a couple of minutes but then I re-factored the code and can't get it working again. I don't have any idea why it stopped working but I am completely stuck on it at the moment. I will mention that I am using Spring to manage Hibernate but I don't think that will be making any difference.
I am using HibernateTool to generate both the hbm.xml and the Java POJOs and have a simple database join as follows:
Code:
CREATE TABLE habitTrackerDomain (
domain_id INT(10) NOT NULL AUTO_INCREMENT,
domain VARCHAR(255) NOT NULL UNIQUE,
PRIMARY KEY (domain_id)
) ENGINE = InnoDB
AUTO_INCREMENT = 500;
CREATE TABLE habitTrackerUser (
user_id INT(10) NOT NULL AUTO_INCREMENT,
user_id_in_domain VARCHAR(255) NOT NULL,
session_id CHAR(36),
PRIMARY KEY (user_id),
INDEX index_user_id_in_domain(user_id_in_domain)
) ENGINE = InnoDB
AUTO_INCREMENT = 500;
CREATE TABLE habitTrackerDomainToUser (
domain_id INT(10) NOT NULL,
user_id INT(10) NOT NULL,
PRIMARY KEY (domain_id , user_id),
CONSTRAINT fk_habitTrackerDomainToUser_domain_id FOREIGN KEY fk_habitTrackerDomainToUser_domain_id(domain_id)
REFERENCES habitTrackerDomain(domain_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT fk_habitTrackerDomainToUser_user_id FOREIGN KEY fk_habitTrackerDomainToUser_user_id(user_id)
REFERENCES habitTrackerUser(user_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT
)
ENGINE = InnoDB;
The generated hbm.xml files are:
Code:
<hibernate-mapping>
<class name="uk.co.prodia.prosoc.domainmodel.HabitTrackerDomain" table="habitTrackerDomain" catalog="habittracker">
<id name="domainId" type="java.lang.Integer">
<column name="domain_id" />
<generator class="identity" />
</id>
<property name="domain" type="string">
<column name="domain" not-null="true" unique="true" />
</property>
<set name="habitTrackerUsers" inverse="false" table="habitTrackerDomainToUser">
<key>
<column name="domain_id" not-null="true" />
</key>
<many-to-many entity-name="uk.co.prodia.prosoc.domainmodel.HabitTrackerUser">
<column name="user_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
and:
Code:
<hibernate-mapping>
<class name="uk.co.prodia.prosoc.domainmodel.HabitTrackerUser" table="habitTrackerUser" catalog="habittracker">
<id name="userId" type="java.lang.Integer">
<column name="user_id" />
<generator class="identity" />
</id>
<property name="userIdInDomain" type="string">
<column name="user_id_in_domain" not-null="true" />
</property>
<property name="sessionId" type="string">
<column name="session_id" length="36" />
</property>
<set name="habitTrackerTrackers" inverse="true">
<key>
<column name="user_id" not-null="true" />
</key>
<one-to-many class="uk.co.prodia.prosoc.domainmodel.HabitTrackerTracker" />
</set>
<set name="habitTrackerDomains" inverse="true" table="habitTrackerDomainToUser">
<key>
<column name="user_id" not-null="true" />
</key>
<many-to-many entity-name="uk.co.prodia.prosoc.domainmodel.HabitTrackerDomain">
<column name="domain_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
and my Java code for saving the HabitTrackerUser object is:
Quote:
HabitTrackerUser habitTrackerUser = new HabitTrackerUser(username);
HabitTrackerDomain habitTrackerDomain = new HabitTrackerDomain(domainName);
habitTrackerDomain.getHabitTrackerUsers().add(habitTrackerUser);
habitTrackerUser.getHabitTrackerDomains().add(habitTrackerDomain); // Must load up both sides in the m-n relentionship for hibernate
save(habitTrackerUser);
It all looks OK to me:
1) The
inverse parameter in the hbm.xml file looks like it is being correctly generated;
2) I have loaded up both sides of the relationship in the Java code so that each object possesses the other object;
3) I have tried to call save on the other object but it complains about the duplicate entry for domain_name, as it should;
4) I have tried to use getCurrentSession().flush() but it doesn't seem to make any difference;
5) I have checked that the objects are correctly populated on both sides just before calling save and they are both OK.
The only table that get populated is the habitTrackerUser table, the habitTrackerDomainToUser table is ignored as is the habitTrackerDomain table if the UNIQUE field restriction on domain_name is removed.
Does anyone have any suggestions that maybe helpful? Thanks in advance.