-->
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: Dynamic Instantiation
PostPosted: Tue Apr 04, 2006 10:20 am 
Newbie

Joined: Tue Apr 04, 2006 9:50 am
Posts: 3
Could someone please shed some light on dynamic instantiation? I actually did manage to get it to work but it doesn't seem to me to be functioning as it should be. I've read a few posts found through a google search - the most interesting making mention of importing a class somewhere, apparently in the hbm.xml file, but I couldn't make that work - and the section in the book, "Hibernate In Action" which says that Hibernate doesn't even have to know about the transient class, but after some testing that hasn't been my experience and I actually needed to create a hbm.xml file for the transient class to get it to work. Has anyone made use of this feature? If so, please comment on it.

Thanks,
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 12:51 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You can import the transient types you want to dynamically instatiate from NHibernate by importing them through the Mappings object (generated by the Configuration object):

Code:
configuration.CreateMappings().AddImport(type.AssemblyQualifiedName, alias);


Normally I loop through all the types I want to be able to dynamically instantiate and call the AddImport method for each one before I instantiate my SessionFactory.

NHibernate doesn't exactly know about the transient type in terms of it being mapped, but it does need to know about the type. I don't think you need to do the import if the type is in the same assembly...but I'm not sure.

Anyway, this works fine for me!

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 1:45 pm 
Newbie

Joined: Tue Apr 04, 2006 9:50 am
Posts: 3
The .CreateMappings.AddImport(...) was the piece of information I was missing. I have modified my code and things are working beautifully...and make sense! Thanks Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 3:40 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
CreateMappings is part of internal API actually (it's undocumented), so don't rely on it too much. It's better to import the class in a mapping file, as it was mentioned, for example like this:
Code:
<hibernate-mapping ...>
    <import class="Fully.Qualified.Class.Name, Assembly" />
</hibernate-mapping>


The reason NHibernate requires classes to be imported while Hibernate doesn't is that the syntax of fully-qualified class names is different in .NET (the assembly part is present), and including an assembly-qualified class name directly in an HQL query would confuse the HQL parser.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 1:29 pm 
Newbie

Joined: Tue Apr 04, 2006 9:50 am
Posts: 3
Thanks for the further clarification, Sergey. I think I will adapt my code to make use of what you have said to ensure I don't have trouble in the future.

-Scott


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.