Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6848

Hibernate foreign key as primary key

Date Sun, 07 Aug 2011 21:38:25 +1000
From Lionel <lionelv@none.com>
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.18) Gecko/20110621 Mandriva/3.1.11-0.1mdv2010.2 (2010.2) Thunderbird/3.1.11
MIME-Version 1.0
Newsgroups comp.lang.java.programmer
Subject Hibernate foreign key as primary key
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
NNTP-Posting-Host 60.241.47.75
X-Original-NNTP-Posting-Host 60.241.47.75
Message-ID <4e3e78a1@dnews.tpgi.com.au> (permalink)
X-Trace dnews.tpgi.com.au!tpg.com.au 1312716961 60.241.47.75 (7 Aug 2011 21:36:01 +1000)
Lines 84
Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news.glorb.com!news.ripco.com!rahul.net!wasp.rahul.net!rahul.net!nntp1.phx1.gblx.net!nntp.gblx.net!nntp.gblx.net!nntp3.phx1!dnews.tpgi.com.au!tpg.com.au!not-for-mail
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6848

Show key headers only | View raw


This may be a little off topic but I'm sure there are some experts in here.

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?

Thanks

Lionel.

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

Hibernate foreign key as primary key Lionel <lionelv@none.com> - 2011-08-07 21:38 +1000
  Re: Hibernate foreign key as primary key markspace <-@.> - 2011-08-07 07:07 -0700
  Re: Hibernate foreign key as primary key Robert Klemme <shortcutter@googlemail.com> - 2011-08-07 18:03 +0200
  Re: Hibernate foreign key as primary key Frank Langelage <frank@lafr.de> - 2011-08-07 18:45 +0200
    Re: Hibernate foreign key as primary key Robert Klemme <shortcutter@googlemail.com> - 2011-08-07 19:14 +0200
      Re: Hibernate foreign key as primary key Lionel <lionelv@none.com> - 2011-08-08 20:11 +1000
        Re: Hibernate foreign key as primary key Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-08 18:26 -0300
          Re: Hibernate foreign key as primary key Lionel <lionelv@none.com> - 2011-08-09 18:56 +1000
            Re: Hibernate foreign key as primary key Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-09 21:45 -0300
        Re: Hibernate foreign key as primary key Lew <lewbloch@gmail.com> - 2011-08-10 09:58 -0700

csiph-web