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.  [ 2 posts ] 
Author Message
 Post subject: Primary key that is also a foreign key
PostPosted: Tue Jan 23, 2007 2:51 pm 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
I'm mapping a legacy database, and I've come across this problem:

I have two tables, Forloeb and GEpj. GEpj has an auto-generated id, and Forloeb's primary key is a foreign key to GEpj's id.

But how can I map this "PK is a FK" i NHibernate? I've tried with a composite id, but this seems... messy.

I've included my composite id attempt, but it generates an error when I try to Get a forloeb object. (identifier type mismatch, Parameter name: id)

Hibernate version:
1.2.0.Beta3

Mapping documents:
Code:
    [Serializable]
    [HibernateMapping(Assembly = "...", Namespace = "...", DefaultLazy = true)]
    [Class(Table = "Forloeb", NameType = typeof(Forloeb))]
    public class Forloeb
    {
        private GEpj mForloebId;

        [CompositeId(0)]
        [KeyManyToOne(1, Name = "ForloebId", Column = "ForloebID", ClassType = typeof(GEpj))]
        public virtual GEpj ForloebId
        {
            get { return mForloebId; }
            private set { mForloebId = value; }
        }

        public override bool Equals(object obj)
        {
            Forloeb f = obj as Forloeb;
            if (f == null)
                return false;

            return f.ForloebId == ForloebId;
        }

        public override int GetHashCode()
        {
            return ForloebId.GetHashCode();
        }

    }


Code:
<?xml version="1.0" encoding="utf-8"?>
<!--Generated by NHibernate.Mapping.Attributes on 2007-01-23 19:15:27Z.-->
<hibernate-mapping namespace="..." assembly="..." default-lazy="true" xmlns="urn:nhibernate-mapping-2.2">
  <class name="[...].Forloeb, [...]" table="Forloeb">
    <composite-id>
      <key-many-to-one name="ForloebId" class="[...].GEpj, [...]" column="ForloebID" />
    </composite-id>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 3:23 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
Have You tried mapping it with <joined-subclass>?

Edit: Or map the primary key twice, with second time with <many-to-one insert="false" update="false">

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.