Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
1.2.0 Beta 1
Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="BCS.Communications.Data.Tables" assembly="BCS.Communications.Data">
<class name="Destination">
<id name ="DestinationId" column="DestinationId" type="int">
<generator class="native" />
</id>
<property name="Address" type="string"/>
<property name="Role" column="Role" type="BCS.Communications.Data.Tables.RoleType, BCS.Communications.Data" length="500" not-null="true" />
<property name="Status" column="Status" type="BCS.Communications.Data.Tables.StatusType, BCS.Communications.Data" length="500" not-null="true" />
<many-to-one name="TransportId" class="Transport" column="TransportId" foreign-key="fk_destination_transport" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="BCS.Communications.Data.Tables" assembly="BCS.Communications.Data">
<class name="Transport">
<id name ="TransportId" column="TransportId" type="int">
<generator class="native" />
</id>
<property name="Description" type="string" />
<property name="Xml" type="string" />
<property name="TransportService" type="BCS.Communications.Data.Tables.ServiceType, BCS.Communications.Data" />
<bag name="Destinations" cascade="all" fetch="join" inverse="true">
<key column="TransportId" foreign-key="fk_destination_transport"/>
<one-to-many class="Destination" />
</bag>
</class>
</hibernate-mapping>
I'm having problems with retrieving an object using Criteria. The problem is if I create a Transport object and add 2 destinations to this object when I use criteria to retrieve the Transport object by Description I get two copies of the same object returned and they both have the two Destination objects in their destination collections. The amount of copies of the Transport object which are returned depends on how many Destinations I add to it. E.g if I add 2 Destination objects to a Transport object I get 2 copys of Transport when I query but if I add 3 Destinations I get 3 copies and so on. Here is the code I'm using to test this.
Code:
public void TestMessageInsert()
{
ISession session = NHibernateHelper.GetCurrentSession();
Transport t = new Transport();
t.Description = "Test";
t.Xml = "";
t.AddDestination(new Destination());
t.AddDestination(new Destination());
session.SaveOrUpdateCopy(t);
ICriteria criteria = session.CreateCriteria(typeof(Transport));
criteria.Add(Expression.Eq("Description", "Test"));
IList<Transport> result = criteria.List<Transport>();
}
[/code]