-->
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.  [ 1 post ] 
Author Message
 Post subject: Foreign key as primary key
PostPosted: Tue Aug 09, 2011 6:40 am 
Newbie

Joined: Tue Jul 05, 2011 6:10 am
Posts: 4
I'm having some troubles getting using a foreign key as a primary key. In this post I will use the most simple issue I'm having trouble with.

Here's some snippets (pharmacy project):

public abstract class AbstractDrugModel
{
private String name;
private IntravascularDrug ivDrug;
}

public class IntravascularDrug
{
private AbstractDrugModel drug;
}

Note 1: I am only just introducing hibernate, previously I did the databases manually and in that scenario IntravascularDrug did not contain an AbstractDrugModel instance.

Note 2: ivDrug can be null.

In my mapping files I have (at this point in time, I've tried other approaches):

<hibernate-mapping>
<class
lazy="false"
name="tciworks.drugmodel.IntravascularDrug"
table="IntravascularDrug">
<composite-id>
<key-many-to-one name="drug" column="DrugName"/>
</composite-id>
...
</class>
</hibernate-mapping>

<hibernate-mapping>
<class
dynamic-insert="false"
dynamic-update="false"
lazy="false"
mutable="true"
name="tciworks.drugmodel.AbstractDrugModel"
optimistic-lock="version"
polymorphism="implicit"
select-before-update="false"
table="Drug">

<id column="Name" name="name"/>
<discriminator column="DrugModelType"/>
...
<many-to-one
cascade="all"
column="DrugName"
name="intravascularDrug"
not-null="false"
unique="true"/>

<subclass
discriminator-value="USER_DEFINED"
name="tciworks.drugmodel.userdefined.UserDefinedDrugModel"/>
</class>
</hibernate-mapping>

This example includes only one concrete sub-class of AbstractDrugModel, there are others. Drug name is unique hence being used as PK.

When I have Hibernate create the database tables everything appears fine, but on save, it seems to be saving IntravascularDrug first and hence gives the following error:

Sun Aug 07 21:03:25 EST 2011: ERROR logExceptions, Cannot add or update a child row: a foreign key constraint fails (`tciworkssqlextension`.`intravasculardrug`, CONSTRAINT `FK19E96D413937CB47` FOREIGN KEY (`DrugName`) REFERENCES `drug` (`Name`))

Does anyone have any suggestions on how I should do this?

I've tried the solution on page 184 of the reference manual and that doesn't work either.

Thanks

Lionel.


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

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.