-->
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.  [ 5 posts ] 
Author Message
 Post subject: NHibernate mapping question
PostPosted: Mon Jun 25, 2007 4:34 pm 
Newbie

Joined: Mon Jun 25, 2007 3:58 pm
Posts: 3
I am new to NHibernate but loved it already. I ran into a problem with the mapping. I have a class that contains a System.Net.Mail.MailAddress object. I am trying to map it as a component of the parent class. How do I tell NHibernate the class of MailAddress?
I tried the following but not working:
<component name ="Sender" class="System.Net.Mail.MailAddress" >
<property name="Address" column="Sender" type="string" length="50" />
<property name="DisplayName" column="SenderName" type="string" length="50" />
</component>

any response will be greatly appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 25, 2007 6:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Please elaborate on how it is not working. Information such as exception message and stack trace will help people help you. At any rate, I believe the class used for a component must have a default constructor, and System.Net.Mail.MailAddress does not have one.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: Re: NHibernate Maping Question
PostPosted: Tue Jun 26, 2007 9:47 am 
Newbie

Joined: Mon Jun 25, 2007 3:58 pm
Posts: 3
Karl, thank you for your reply. actually, it didn't even go that far, the mapping file could not be compiled with the following error:

{"could not find class: System.Net.Mail.MailAddress"}

The stack trace:

at NHibernate.Cfg.HbmBinder.ClassForFullNameChecked(String fullName, String errorMessage)
at NHibernate.Cfg.HbmBinder.ClassForNameChecked(String name, Mappings mappings, String errorMessage)
at NHibernate.Cfg.HbmBinder.GetPropertyType(XmlNode definingNode, Mappings mappings, Type containingType, String propertyName)
at NHibernate.Cfg.HbmBinder.PropertiesFromXML(XmlNode node, PersistentClass model, Mappings mappings)
at NHibernate.Cfg.HbmBinder.BindRootClass(XmlNode node, RootClass model, Mappings mappings)
at NHibernate.Cfg.HbmBinder.BindRoot(XmlDocument doc, Mappings mappings)
at NHibernate.Cfg.Configuration.AddValidatedDocument(XmlDocument doc, String name)



I also tried to remove the class="System.Net.Mail.MailAddress" part and got the following error:

"Could not find a setter for property 'Address' in class 'System.Net.Mail.MailAddress'"

with stack trace:
at NHibernate.Property.BasicPropertyAccessor.GetSetter(Type type, String propertyName)
at NHibernate.Mapping.Property.GetSetter(Type clazz)
at NHibernate.Cfg.HbmBinder.BindComponent(XmlNode node, Component model, Type reflectedClass, String className, String path, Boolean isNullable, Mappings mappings)
at NHibernate.Cfg.HbmBinder.PropertiesFromXML(XmlNode node, PersistentClass model, Mappings mappings)
at NHibernate.Cfg.HbmBinder.BindRootClass(XmlNode node, RootClass model, Mappings mappings)
at NHibernate.Cfg.HbmBinder.BindRoot(XmlDocument doc, Mappings mappings)
at NHibernate.Cfg.Configuration.AddValidatedDocument(XmlDocument doc, String name)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 10:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
There is no setter for the Address property, therefore, unless you know the underlying field name (if it even uses one), you cannot get NHibernate to map to it.

Based on the problem with the Address property and the fact that MailAddress does not have a default constructor, it may not be the best of ideas to map directly to the MailAddress class. My suggestion is to map to a couple of private fields and then create helper methods or accessors to get and set the Sender property. What you lose from doing that is the ability to query on that property like this:
Code:
from Email e where e.Sender.DisplayName = :name

Instead, you will have to write this (assuming the field name is senderDisplayName):
Code:
from Email e where e.senderDisplayName = :name

An alternative is to extend the MailAddress class and give it what it is missing. I would advise against it though.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: Re: NHibernate Mapping Question
PostPosted: Tue Jun 26, 2007 10:17 am 
Newbie

Joined: Mon Jun 25, 2007 3:58 pm
Posts: 3
Thank you so much Karl. You have pointed me to the right direction.


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