Hibernate version: 1.2.o Beta 1
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="FIXMESSAGE" assembly="FIXMESSAGE" >
<class name="MessageX">
<id name ="MessageId">
<column name="MessageId" sql-type="int" />
<generator class="native" />
</id>
<bag name="Destinations" cascade="all" fetch="select" lazy="true" inverse="true">
<key column="MessageId" foreign-key="fk_destination_message" />
<one-to-many class="Destination" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="FIXMESSAGE" assembly="FIXMESSAGE">
<class name="Destination">
<id name ="DestinationId" column="DestinationId" type="int">
<generator class="native" />
</id>
<property name="Address" type="string"/>
<many-to-one name="MessageId" class="MessageX" column="MessageId" foreign-key="fk_destination_message" fetch="join" />
</class>
</hibernate-mapping>
Code:
public void testConnection()
{
try
{
ISessionFactory factory = new Configuration().Configure().BuildSessionFactory();
ISession session = factory.OpenSession();
MessageX m = new MessageX();
m.AddDestination(new Destination());
m.AddDestination(new Destination());
session.Save(m);
MessageX ms = session.Get<MessageX>(1);
IQuery query = session.CreateQuery("from MessageX message inner join fetch message.Destinations");
IList<MessageX> result = query.List<MessageX>();
session.Close();
}
catch (Exception e)
{
}
}
I have a problem with Nhibernate. When I use the above mapping to create a very simple one to many relationship the database and tables etc are created fine and the proper relationships mapped etc. I can then insert data into the tables and its inserted in the correct way. If I then use lazy loading to select the data this works fine. The problem I'm having is that if I use an HQL query to join the two tables, instead of getting one MessageX which has two destination child objects I get two identical MessageX which each have two Destination child objects. This changes depending on how many Destination objects a message object refers to e.g if I added 3 Destination child objects to the MessageX I'd get 3 copies when I queried.
[/code]