-->
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: Composite primary key/one-to-one
PostPosted: Mon Feb 12, 2007 6:02 pm 
Newbie

Joined: Mon Feb 12, 2007 5:36 pm
Posts: 4
Version: Nhibernate 1.2b3.
I have a composite key (2 columns) that has one-to-one mapping to child table and further. The one-to-one relation continues over 3 or 4 tables down and back (bidirectional).
The column names differ from table to table.

I have tested now on 2 tables with a helper class "ObjItemStatComp".
When I load the tables my primary key is 0 (HQL: Select o from ObjItemstat o).
Where's the trick to get it working?

Parent table:


<class name="C2.DAL.DaoObjects.ObjItemStat,C2" table="OBJ_ITEM_STAT" lazy="true">

<!-- Created composite-id element for primary-keys. -->
<composite-id access="field" class="C2.DAL.DaoObjects.ObjItemStatComp,C2">
<key-property name="ObjItemId" column="OBJ_ITEM_ID" type="Decimal" />
<key-property name="ObjItemStatIx" column="OBJ_ITEM_STAT_IX" type="Decimal" />
</composite-id>

<one-to-one name="FacStatChild" class="C2.DAL.DaoObjects.FacStat,C2" />
...
</class>

--------------------------

Child table:


<class name="C2.DAL.DaoObjects.FacStat,C2" table="FAC_STAT" lazy="true">

<!-- Created composite-id element for primary-keys. -->
<composite-id access="field" class="C2.DAL.DaoObjects.ObjItemStatComp,C2">
<key-property name="ObjItemId" column="FAC_STAT_ID" type="Decimal" />
<key-property name="ObjItemStatIx" column="OBJ_ITEM_STAT_IX" type="Decimal" />
</composite-id>

<!-- Second part of one-to-one element of composite-id. -->
<one-to-one name="ObjItemStatParent" class="C2.DAL.DaoObjects.ObjItemStat,C2" constrained="true" />
...
</class>

------------------------------------

[Serializable]
public class ObjItemStatComp
{
#region Private Members
private Decimal m_obj_item_stat_ix;
private Decimal m_obj_item_id;
#endregion // Private Members

#region Default ( Empty ) Class Constructor
public ObjItemStatComp()
{
m_obj_item_stat_ix = 0;
m_obj_item_id = 0;
}
#endregion // End of Default ( Empty ) Class Constructor

#region Public Properties
public virtual Decimal ObjItemStatIx
{
get { return m_obj_item_stat_ix; }
set { m_obj_item_stat_ix = value;
}
}

public virtual Decimal ObjItemId
{
get { return m_obj_item_id; }
set { m_obj_item_id = value;
}
}
#endregion // Public Properties

#region Equals And HashCode Overrides
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
ObjItemStatComp castObj = (ObjItemStatComp)obj;
return ( castObj != null ) &&
( this.m_obj_item_stat_ix == castObj.ObjItemStatIx ) &&
( this.m_obj_item_id == castObj.ObjItemId );
}

public override int GetHashCode()
{

int hash = 57;
hash = 27 * hash * m_obj_item_stat_ix.GetHashCode();
hash = 27 * hash * m_obj_item_id.GetHashCode();
return hash;
}
#endregion
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 9:06 pm 
Newbie

Joined: Mon Feb 12, 2007 5:36 pm
Posts: 4
Fixed it myself. I discovered that access="field" is more important than I thought. I skipped it and voilĂ .


Top
 Profile  
 
 Post subject: new question on one-to-one mapping
PostPosted: Tue Feb 13, 2007 5:28 pm 
Newbie

Joined: Mon Feb 12, 2007 5:36 pm
Posts: 4
New question:

I have a parent->child->child relation (one-to-one)
How is it mapped in general ?


All refer to (root) parent table ?


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.