I'm new to NHibernate and I'm trying to get my head around the use of
composite-id. I can get everything to work except SaveAndUpdate. I'm
using verison 2.2.
Table DDL is (unfortunately this can not change)
CREATE TABLE `argon`.`layer` (
`LAYERID` bigint(20) unsigned NOT NULL,
`CENTREID` bigint(20) unsigned NOT NULL,
`SERVERID` bigint(20) unsigned NOT NULL,
`DESCRIPTION` varchar(256) DEFAULT NULL,
KEY `Index_Layer` (`LAYERID`,`CENTREID`)
)
My mapping file so far is
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Argon.Admin"
namespace="Argon.Admin">
<class name="Layer">
<composite-id>
<key-property name="LayerId"/>
<key-property name="CentreId"/>
</composite-id>
<property name="ServerId" />
<property name="Description" />
</class>
<query name="Layer.get_by_layer">from Layer where LayerId=:layer</
query>
<query name="Layer.get_by_centre">from Layer where CentreId=:centre</
query>
<query name="Layer.get_by_layer_and_centre">from Layer where
(LAYERID=:layer) and (CENTREID=:centre)</query>
</hibernate-mapping>
And my code so far is
[Serializable]
public class Layer
{
public virtual long LayerId { get; set; }
public virtual long CentreId { get; set; }
public virtual long EMSServerId { get; set; }
public virtual string Description { get; set; }
public override Equals(Object aObject) {
...code omitted for brevity
}
public override int GetHashCode()
{
return (int)((LayerId * 1000) + CentreId);
}
public static Layer Save(Layer aLayer)
{
// Just a convienience call - if record does not exist
then null returned.
Layer existing_layer = GetByLayerIDAndCentreID
(aLayer.LayerId, aLayer.CentreId);
using (ISession session = OpenSession())
{
if (existing_layer == null)
{
session.Save(aLayer);
}
else
{
session.Update(aLayer);
}
session.Flush();
return aLayer;
}
}
... rest of code omitted for brevity
}
Now this works but to me the Save method seems unweildy as I'm able
just to use a SaveOrUpdate for all my other classes that have a simple
primary key. If I try SaveOrUpdate then NH just does an update, fine
for when an update is needed but fails for a new record.
Reading the docs suggests that I can create a separate class to
implement the key and refer to it in the composite-id element in the
mapping. But when I do that the LayerID and CentreID fields of the
Layer class do not get used, so for a method that gets data this end
up blank.
I was wondering if someone could give a clear example of the best (or
a better) way of doing this assuming the database can not change.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---