-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping Problem
PostPosted: Wed May 09, 2007 7:37 am 
Newbie

Joined: Tue May 08, 2007 11:52 am
Posts: 6
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 ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 4:14 pm 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Not sure if this will fix everything, but you're going to need to override Equals and GetHashCode in RelAB. (per 5.15 of the documentation: Your persistent class must override Equals() and GetHashCode() to implement composite identifier equality. It must also be Serializable. )

Can you paste the full error?


Top
 Profile  
 
 Post subject: Mapping Problem
PostPosted: Thu May 10, 2007 5:42 am 
Newbie

Joined: Tue May 08, 2007 11:52 am
Posts: 6
Thanks for your reply.
I forgot to put that, but i already override those methods.

My big problem is that i want to map the relation between A and B in RelAB. That table has extra attributes.

I've tried everything, but i can't get it work.

Is realy necessary to have the class RelAB to map the extra columns ?
Is realy necessary to have a composite element.

I don't know if the problem is in the mapping files, or in the classes.

If anyone has an example how to implement it i would apreciate the help.

The error occurs when i try to access the collection RelAB in class A. The first error is "failed to initialize laizy collection."


Thanks


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