Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Hibernate foreign key as primary key Date: Sun, 07 Aug 2011 18:03:02 +0200 Lines: 107 Message-ID: <4E3EB736.2010208@googlemail.com> References: <4e3e78a1@dnews.tpgi.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net pZ06hya87cHUMrYO8WzM6wHbuD+BqA/syARPdQMiszzTXtOJg= Cancel-Lock: sha1:W5NjVR7BThpIN1tx/RcHu7PZk18= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <4e3e78a1@dnews.tpgi.com.au> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6853 On 07.08.2011 13:38, Lionel wrote: > 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): > > > lazy="false" > name="tciworks.drugmodel.IntravascularDrug" > table="IntravascularDrug"> > > > > ... > > > > > 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"> > > > > ... > cascade="all" > column="DrugName" > name="intravascularDrug" > not-null="false" > unique="true"/> > > discriminator-value="USER_DEFINED" > name="tciworks.drugmodel.userdefined.UserDefinedDrugModel"/> > > > > 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? With Oracle you could mark the FK as "deferrable initially deferred" which basically means that the constraint is checked at commit time and not when updates happen. Maybe your RDBMS supports this feature as well. My Hibernate foo is a bit rusty but I think that is the only way to solve this with the current class layout because if you have a bidirectional one to one relationship one record has to be inserted into the database first - which means that the other one isn't there. And if the FK field is not allowed to be NULL on both sides then this can never work (unless, again, the RDBMS supports deferred constraints). I don't know if Hibernate is smart enough to figure insertion order if one of the two sides is allowed to be NULL. What do the docs say? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/