These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: SOLVED: A foreign key constraint fails
PostPosted: Thu Jan 07, 2010 5:33 pm 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
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 Tracker
Code:
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 TrackerConfiguration
Code:
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?


Last edited by klogger on Thu Jan 07, 2010 8:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: A foreign key constraint fails
PostPosted: Thu Jan 07, 2010 7:09 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi klogger,

klogger wrote:
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>



Have you tried to set the cascade attribute in the set element of the mapping. You might want to check out Transitive Persistence in the Hibernate Docs.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: A foreign key constraint fails
PostPosted: Thu Jan 07, 2010 7:42 pm 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
Thanks for taking the time to reply Froestel.

I did try to set cascade to 'all' as well as 'save-update' and 'persist' on both sides of the relentionship but it didn't help. I think you have pointed me in the right direction though. The Transitive persistence does sound like it is causing the issue here, I am just a little unsure why setting the cascade option didn't work.


Top
 Profile  
 
 Post subject: Re: A foreign key constraint fails
PostPosted: Thu Jan 07, 2010 8:33 pm 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
OK, you were right. I had gotten the parent/child relentionship the wrong way round. It is my TrackerConfiguration that is the parent so I needed to call:

Code:
save(trackerConfiguration);


where I was calling:

Code:
save(tracker);


and that saves the Tracker child. I also needed the cascade="persist,merge,save-update" that you mentioned.

Many thanks for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.