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>