Hi, i'm a newbie in NHibernate. I've a question tham maybe seems stupid, but i can't get it work.
I've 3 tables
A <-> RelAB <-> B
Table A
CodA Guid PrimaryKey
xxx
yyy
Table B
CodB Guid PrimaryKey
ooo
zzz
Table B
CodA Guid ForeignKey (To A)
CodB Guid ForeignKey (To B)
Date
InsertedBy
I've created 3 mapping files and 3 classes to handle it
Classes :
Classe A
class A {
private Guid CodA;
private String xxx;
private String yyy;
private System.Collections.Generic.ICollection<RelAB> relacoesSet;
public virtual System.Collections.Generic.ICollection<RelAB> RelacoesSet
{
get
{
return this.relacoesSet;
}
set
{
this.relacoesSet = value;
}
}
public Entidade()
{
}
...
Classe B
class B {
private Guid CodB;
private String ooo;
private String zzz;
private System.Collections.Generic.ICollection<B> relacoesSet;
public virtual System.Collections.Generic.ICollection<B> RelacoesSet
{
get
{
return this.relacoesSet;
}
set
{
this.relacoesSet = value;
}
}
public Entidade()
{
}
...
Class RelAB
class RelAB
{
private DateTime date;
private string insertedBy;
public virtual DateTime Date
{
get { return date; }
set { date = value; }
}
public virtual string InsertedBy
{
get { return insertedBy; }
set { insertedBy = value; }
}
private A a;
public virtual A ToA
{
get
{
return this.a;
}
set
{
this.a = value;
}
}
private B b;
public virtual B ToB
{
get
{
return this.b;
}
set
{
this.b = value;
}
}
public RelAB()
{
}
}
Mapping Files
a.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="xxxxx.A, xxxxx" table="A">
<id name="CodA" type="Guid">
<generator class="guid" />
</id>
<property name="xxx" type="string" length="256"/>
<property name="yyy" type="String" length="256"/>
<bag name="RelacoesSet"
inverse="true"
lazy="true"
cascade="none">
<key column="CodA"/>
<one-to-many class="xxxxx.RelAB, xxxxx"/>
</bag>
</class>
</hibernate-mapping>
b.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="xxxxx.B, xxxxx" table="B">
<id name="CodB" type="Guid">
<generator class="guid" />
</id>
<property name="ooo" type="string" length="256"/>
<property name="zzz" type="String" length="256"/>
<bag name="RelacoesSet"
inverse="true"
lazy="true"
cascade="none">
<key column="CodB"/>
<one-to-many class="xxxxx.RelAB, xxxxx"/>
</bag>
</class>
</hibernate-mapping>
relab.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="xxxxx.RelAB, xxxxx" table="RelAB">
<composite-id>
<key-many-to-one name="CodA" class="A" column="CodA" />
<key-many-to-one name="CodB" class="B" column="CodB" />
</composite-id>
<property name="Date" type="String" length="20"/>
<property name="InsertedBy" type="String" length="20"/>
</class>
</hibernate-mapping>
The problem is in my composite id. I've tried with
<key-property name="CodA" type="Guid"/>
<key-property name="CodB" type="Guid"/>
but i can't get it work. NHibernate gives me the error "Could not compile the mapping document: RelAB.hbm.xml"
If i set just an id it works.
How can i mapping the relation to A, B and RelAB in classes and in hbm files.
I aprecciate any help.
P.S.- Sorry for my English ;)
|