I have looked all over for the answer to this and haven't been successful but I am sure the solution is simple!
I have two tables
Table TrackerCode:
CREATE TABLE IF NOT EXISTS `Tracker` (
`tracker_id` INT NOT NULL AUTO_INCREMENT ,
`configuration_id` INT ,
PRIMARY KEY (`tracker_id`) ,
INDEX `fk_Tracker_configuration_id` (`configuration_id` ASC) ,
CONSTRAINT `fk_Tracker_configuration_id`
FOREIGN KEY (`configuration_id` )
REFERENCES `TrackerConfiguration` (`configuration_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table TrackerConfigurationCode:
CREATE TABLE IF NOT EXISTS `TrackerConfiguration` (
`configuration_id` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`configuration_id`) )
These are the auto-generated by hibernate-tool:
Code:
<class name="uk.co.prodia.prosoc.domainmodel.Tracker" table="Tracker" catalog="habittracker">
<id name="trackerId" type="java.lang.Integer">
<column name="tracker_id" />
<generator class="identity" />
</id>
<many-to-one name="TrackerConfiguration" class="uk.co.prodia.prosoc.domainmodel.TrackerConfiguration" fetch="select">
<column name="configuration_id" not-null="true" />
</many-to-one>
</class>
Code:
<class name="uk.co.prodia.prosoc.domainmodel.TrackerConfiguration" table="TrackerConfiguration" catalog="habittracker">
<id name="configurationId" type="java.lang.Integer">
<column name="configuration_id" />
<generator class="identity" />
</id>
<set name="Trackers" inverse="true" lazy="true" table="Tracker" fetch="select">
<key>
<column name="configuration_id" not-null="true" />
</key>
<one-to-many class="uk.co.prodia.prosoc.domainmodel.Tracker" />
</set>
</class>
I load both sides of the relentionship so that:
Code:
tracker.setTrackerConfiguration(trackerConfiguration);
trackerConfiguration.setTrackers(hashSetOfTrackers);
save(tracker)
I can only get this to work if I:
- First extract the TrackerConfiguration from the Tracker;
- call save(trackerConfiguration);
- call save(tracker).
Without first saving the child, I can't save the parent as the child's ID is
null. Any ideas please?