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.  [ 2 posts ] 
Author Message
 Post subject: Retrieving "length" attribute from mappings
PostPosted: Fri Oct 06, 2006 3:39 am 
Newbie

Joined: Wed Jun 07, 2006 3:15 am
Posts: 14
I always wondered why someone should provide a length attribute for string properties in mapping files. Is it used by nhibernate anyplace?

Anyway I wrote this little function someone can find usefull. If you have any better ways to do that please let me know (I checked the GetClassMetadata function but I don't think it holds that info.)

Code:
RETRIEVING NOT-NULL AND LENGTH ATTRIBUTES

Assembly assembly = this.GetType().Assembly;
string[] resourceNames = assembly.GetManifestResourceNames();

foreach (string resourceName in resourceNames)
{
   if (resourceName.Contains(this.GetType().Name + ".hbm.xml"))
   {
      xmlMapping = new XmlDocument();
      xmlMapping.Load(assembly.GetManifestResourceStream(resourceName));
   }
}

string xpath = "/urn:hibernate-mapping/urn:class/urn:property[@name='{0}']/urn:column";

XmlNamespaceManager nsmgr = new XmlNamespaceManager(this.xmlMapping.NameTable);
nsmgr.AddNamespace("urn", "urn:nhibernate-mapping-2.0");

XmlNode node = this.xmlMapping.SelectSingleNode(String.Format(xpath, property.Name), nsmgr);

int itemp; bool btemp;

if (Int32.TryParse(node.Attributes["length"].Value, out itemp))
{
   length = itemp;
}

if (Boolean.TryParse(node.Attributes["not-null"].Value, out btemp))
{
   notNull = btemp;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 07, 2006 12:17 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
I always wondered why someone should provide a length attribute for string properties in mapping files. Is it used by nhibernate anyplace?

As far as I know, the length property in the NH Mapping files is used when you do a schema export to have NH create the database tables from the Model/Mapping files.

-devon


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