-->
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.  [ 6 posts ] 
Author Message
 Post subject: one-to-one mapping question
PostPosted: Thu Jul 03, 2008 4:35 pm 
Newbie

Joined: Thu Jun 12, 2008 5:09 pm
Posts: 5
Location: Dublin, Ireland
I am trying to map a Member to a Member Address as a one-to-one mapping. The Member table contains the field AddressID which stores the value of the Primary key of the Members address from the Address table. A member dosent have to have an address. I have tried using the examples to find a solution but I keep getting the error "An association from the table Members refers to an unmapped class: Address" This is my member and address mapping

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Core.Domain.Member, Test.Core" table="Members" lazy="false">
<id name="MemberID" column="MemberID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="UserName" column="UserName" type="String" />
<one-to-one name="Address" class="Test.Core.Domain.Address, Test.Core" />
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Core.Domain.Address, Test.Core" table="Address" lazy="false">
<id name="AddressID" column="AddressID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="HouseNumber" column="HouseNumber" type="Int32" />
<property name="Street" column="Street" type="String" />
<property name="City" column="City" type="String" />
<property name="County" column="County" type="String" />
<property name="State" column="State" type="String" />
<property name="Postcode" column="Postcode" type="String" />
<property name="Country" column="Country" type="String" />
</class>
</hibernate-mapping>

Can anyone help me to get this working? Do I need to update the Address mapping file also?
Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 3:01 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
an association from the table Members refers to an unmapped class: Address


This message sounds more like hibernate doesn't know this class. Can you read/write Address entities ?

Quote:
A member dosent have to have an address.


Afaik that's not possible with a one-to-one. Try a many-to-one instead with unique="true":

Code:
<many-to-one name="Address" class="Test.Core.Domain.Address, Test.Core" unique="true" column="member_id"/>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 06, 2008 10:53 am 
Newbie

Joined: Thu Jun 12, 2008 5:09 pm
Posts: 5
Location: Dublin, Ireland
Ok, I have tried simply reading from the address table with the following error:

NHibernate.MappingException: Unknown entity class: Test.Core.Domain.Address

I realise my problem is that hibernate does not know about my Address class. I can't figure out why. I have created a simple Address class with a simple mapping file in the same way as my Member class and I have no problem to read the data from the Member table. Can you help me to figure this out please?

This is my method for displaying all Members:

private void DisplayAllMembers()
{
IDaoFactory daoFactory = new NHibernateDaoFactory();
IMemberDao memberDao = daoFactory.GetMemberDao();

grdMembers.DataSource = memberDao.GetAll();
grdMembers.DataBind();
}

and this one for displaying all Addresses:

private void DisplayAllAddresses()
{
IDaoFactory daoFactory = new NHibernateDaoFactory();
IAddressDao addressDao = daoFactory.GetAddressDao();

grdAddresses.DataSource = addressDao.GetAll();
}

IDAOFactory interface contains the following:

using Test.Core.Domain;

namespace Test.Core.DataInterfaces
{
public interface IDaoFactory
{
IAddressDao GetAddressDao();
IMemberDao GetMemberDao();
}

#region Inline interface declarations

public interface IMemberDao : IDao<Member> { }

public interface IAddressDao : IDao<Address> { }

#endregion
}

IDAO interface contains the following:

using System.Collections.Generic;

namespace Test.Core.DataInterfaces
{
public interface IDao<T>
{
List<T> GetAll();
}
}

NHibernateDaoFactory class contains the following:

using Test.Core.DataInterfaces;
using Test.Core.Domain;

namespace Test.Data
{
public class NHibernateDaoFactory : IDaoFactory
{
public IMemberDao GetMemberDao()
{
return new MemberDao();
}

public IAddressDao GetAddressDao()
{
return new AddressDao();
}

#region Inline DAO implementations

public class MemberDao : AbstractNHibernateDao<Member>, IMemberDao { }

public class AddressDao : AbstractNHibernateDao<Address>, IAddressDao { }

#endregion
}
}

AbstractNHibernateDao class contains the following:

using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Expression;
using Test.Core.DataInterfaces;


namespace Test.Data
{
public abstract class AbstractNHibernateDao<T> : IDao<T>
{
public List<T> GetAll()
{
return GetByCriteria();
}

public List<T> GetByCriteria(params ICriterion[] criterion)
{
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);

foreach (ICriterion criterium in criterion)
{
criteria.Add(criterium);
}

return criteria.List<T>() as List<T>;
}

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 06, 2008 11:09 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
How do you add your mapping files ?

- in the configuration file >> is Address.hbm.xml added there
- via cfg.AddClass/AddAssembly >> is Address.hbm.xml marked as "EmbeddedRessource" in VS
- cfg.AddFile("Address.hbm.xml")

Check the debug log if the mapping file is read.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 5:01 pm 
Newbie

Joined: Thu Jun 12, 2008 5:09 pm
Posts: 5
Location: Dublin, Ireland
I got it, The mapping file was not in the Test.Core.csproj file as

<EmbeddedResource Include="Domain\Address.hbm.xml">
</EmbeddedResource>
I didn't have to add the Member.hbm.xml as it was there already. Why was the Member mapping there and not the Address?

Thanks for your help. I think I need to spend some more time getting this right before I move on.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 2:31 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
When you add a new xml to your project, it defaults to "Content" I think. You have to change it manually. I forgot this so many times myself ...

_________________
--Wolfgang


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