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: Discovering the NHibernate meta model ...
PostPosted: Thu Jun 01, 2006 6:30 am 
Newbie

Joined: Sat Jan 28, 2006 11:35 am
Posts: 7
Hibernate version:1.0.2

Hi,


I am working on a project that uses nhibernate as the OR mapper. In the context of some prototyping of search facilities in our application, I was looking into the NHibernate.Mapping namespace classes, to see how we can use the metadata model of NHibernate to configure search options in our searches (in particular, the properties of objects that the user can search on ...).

The search functionality should be quite advanced, and the configuration of a search on a particular type of object should allow for the filtering on properties of associated objects (say something like "give me the persons on our system that drive a BMW and have a blank accident record").

So I was writing some simple tests to see if I would be able to retrieve enough information to use it in that kind of configurations.

This is where I've gotten this far: starting from the Configuration, I can run over the ClassMappings collection. For each of these mappings, I can run over the properties, and display some info about them.

The latter action runs fine for value type properties. For associations, it depends on the type of association:

    A one to one association can be detected using
    Code:
    Property.Type.IsAssociationType
    together with
    Code:
    Property.Type.IsEntityType

    A one to many association can be detected using
    Code:
    ((Bag)Property.Value).IsOneToMany



However, I can't find in the object model how I have to discover the concrete type of objects stored in the collection property representing a many to many association. I'm probably looking over it, but I didn't find a way to do this until now.

A similar problem goes for the association mapped as a bag of composite elements. Also in this case, I find the property representing the association in the meta model, but I am not able to determine the actual type stored in the bag.

Some help on this would be much appreciated ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 7:51 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
collection.Element.Type should give you the IType, and IType.ReturnedClass should give you the corresponding System.Type.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:10 am 
Newbie

Joined: Sat Jan 28, 2006 11:35 am
Posts: 7
sergey wrote:
collection.Element.Type should give you the IType, and IType.ReturnedClass should give you the corresponding System.Type.


Thank you for the quick reply.

Unfortunately, in the cases I was referring to, Element is null, so I can't query for it's Type ...

In the cases where I could find the information, I did use the Element to find the information.

Maybe I should have included the testing code I used. This is the relevant snippet:

Code:
         foreach(RootClass root  in _configuration.ClassMappings)
         {
            Console.WriteLine(root.Name);

            foreach(Property property in root.PropertyCollection)
            {
               Console.Write("\t");
               
               if(property.Type.IsAssociationType) Console.Write("Association: ");

               Console.Write(property.Name);
               if( ! property.Type.IsAssociationType ) Console.Write(" (" + property.Type.Name + ")");
               else if( property.Type.IsAssociationType && property.Type.IsEntityType ) Console.Write(" (" + property.Type.Name + ")");
               else
               {
                  try
                  {
                     Bag bag = ((Bag)property.Value);
                     if(bag.IsOneToMany)
                     {
                        Console.Write(" (Collection of " + bag.Element.Type.Name + ") ");
                     }
                     else
                     {
                        Console.Write(" (Collection of ?) ");
                     }
                  }
                  catch
                  {
                     Console.Write("ERROR");
                  }
               }
               Console.Write("\n");
            }
         }


In the two cases that lead to problems, I end in the else statement that writes "Collection of ?" to the console ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:34 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Hmm, you should probably switch to using public instead of internal APIs :) There's ISessionFactory.GetClassMetadata call which returns IClassMetadata object which you can query for the exact IType of any property.

To get the element type from a collection you have to use ISessionFactory.GetCollectionMetadata(string role) which returns an ICollectionMetadata which knows about indexes, elements, and so on. The "role" has the format of "SomeNamespace.EntityClassName.CollectionProperty", and you can also get it by using ((NHibernate.Type.CollectionType) propertyType).Role;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 9:41 am 
Newbie

Joined: Sat Jan 28, 2006 11:35 am
Posts: 7
Definitely the way to go! Works like a charm!

Thank you!!!!!!


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.