NHibernate enum mapping - nhibernate

Can anybody tell me how to map enum in nhibernate using vb.net.When i search the google i found all samples in c# but i want vb.net(i am new to vb.net)
Thanks

Are you using Fluent NHibernate? If not, the XML mappings are identical.

Just create a property node in your xml mapping file which which has the same name as the Property itself. NHibernate will pick up the type (in that case the enum) automatically using reflection.
<property name="YourEnumPropertyName" />

Related

NH 3.2 Fluent Mapping Lazy Loading

I used NH 3.2 mapping by code and I tryied Nhibernate Mapping Generator http://nmg.codeplex.com/ which looked a great tool.
I found a big difference between my code and theirs. On each class they have a call to the function LazyLoad(). (Although I thinked that it was the default behaviour)
Now I fear that my application doesn't use lazy loading, does someone know the default behaviour of nh 3.2 with mapping by code ? (when we don't call the LazyLoad method)
Regards
Depends on the default-lazy attribute of the hibernate-mapping tag which can be changed in Fluent NHibernate by adding the DefaultLazy.Always() or DefaultLazy.Never() convention.
If no default-lazy attribute is defined (no convention added in Fluent NHibernate), lazy loading is enabled.

Can I give an nhibernate session factory a list of mappings to load?

Right now, the session factory just finds all .hbm.xml files embedded in the current assembly it seems. I now have a situation where I only want the session factory to load the list of mappings that I specify. How can I do this?
Thanks,
Isaac
You can use the Configuration.AddResources(IEnumerable<string> paths, Assembly assembly) and specify a desired list of mappings, do your own filtering if you insist having the mappings embedded in the same assembly. Otherwise I would recommend Sergio's answer.
You can use a static method on Configuration class to return a list of available mappings in an assembly, then you can remove the ones you don't want:
var mappings = Configuration.GetAllHbmXmlResourceNames(assembly);
// TODO: filter mappings
cfg.AddResources(mappingsFiltered, assembly);
Well, in the configuration of NHibernate, you specify the assembly where you have embedded your mappings right? What I would suggest is change that configuration dynamically based on your needs.
Another way to do it at run-time would be using the NHibernate.cfg.Configuration.CreateMappings method to create the mappings dynamically. This would require you to create the mappings either on the fly (you can read from a DB or files or something). I personally haven't done this way but I think you could give it a try to solve your needs.
Hope this helps.

Nhibernate: Get real entity class instead of proxied class [duplicate]

This question already has an answer here:
NHibernate Get objects without proxy
(1 answer)
Closed 2 years ago.
Is there a way to make nhibernate return my class instead of its proxy class? I dont mind if it's not lazy or cant be updated.
You can unproxy class with this code
session.PersistenceContext.Unproxy(proxiedInstance)
You should define this in your mapping, by defining lazy="false"
<class name="MyEntity" table="MyTable" lazy="false">
</class>
You can use following code to get real object
InstanceType instance;
if (proxiedInstance is INHibernateProxy)
{
var lazyInitialiser = ((INHibernateProxy)proxiedInstance).HibernateLazyInitializer;
instance = (InstanceType)lazyInitialiser.GetImplementation();
}
I'm using AutoMapper to achieve something similar in Entity Framework.
var nonProxiedInstance = Mapper.DynamicMap<YourType>(proxiedInstance);
That would work if you do not have navigation properties. Otherwise, you'll need to configure a mapping to ignore those properties.
Note: This is (obviously) an inefficient solution.
You can use the technique described in http://sessionfactory.blogspot.com/2010/08/hacking-lazy-loaded-inheritance.html (you'll need to do it recursively)
session.PersistenceContext.Unproxy(proxiedInstance) will not unproxy the associations. Also the no-proxy lazy loading does the same thing.
Disabling lazy loading is not a good idea and AutoMapper would navigate all the properties and trigger the loading mechanism.
IUnitOfWork.Unproxy from the NHUnit package could be used to unproxy the object and it's relations. This method will not initialize any proxy objects by mistake.

FluentNhibernate dynamic runtime mappings

I am building a framework where people will be able to save items that the created by inheriting a class of mine. I will be iterating over every type in the appdomain to find classes that I want to map to nhibernate. Every class that I find will be a subclass of the inherited type.
I know how to create sub types in FluentNhibernate, but every sub type requires its own ClassMap class. Since I won't know these untill runtime, there is no way I can do that.
Is there a way that I can add mappings to fluent nhibernate?
Note, I know this is possible without fluent nhibernate using the Cfg class, but I don't want to manage the same code two different ways.
something along the lines
Type classToMap = GetClassToMap();
var subclassmap = typeof(SubClassMap<>).MakeGenericType(classToMap);
foreach(var item in classToMap.GetPropertiesToMapSomehow())
{
var expression = // build lambda of property
subclassMap.Map(expression).Column("col") ...
}
config.Add(subclassmap) // NHibernate.Cfg.Configuration
There was support for this at once time, and the api is still there, but it is now depricated.

Error mapping UserType from to property with NHibernate

I needed a way to trim strings within my persistent class because my legacy database is using char fields. I downloaded the nHhaddIns dll to use its TrimString class which is derived from IUserType.
Using their example I created a property in my mapping class as shown at the bottom.
uNHAddIns is added as a project within my solution. However, I received this error:"Could not determine type for: uNhAddIns.UserTypes.TrimString, uNhAddIns, for columns: NHibernate.Mapping.Column(HSTAT)"
I tried running the example that is in the uNhAddIns project and receive the same error. Any ideas?
<property name="HSTAT" column="HSTAT" type="uNhAddIns.UserTypes.TrimString, uNhAddIns" />
Don't know if you've managed to fix this already, but does your own uNhAddIns.UserTypes.TrimString inherit from IUserType? My own pattern for user types in NHibernate involves the type implementation living in the DataModel, and the required IUserType interface living separately in my DataAccess layer. The IUserType implementation does the necessary marshalling between the database and my DataModel type implementation.
I just came across this same error when trying to use the DataModel class in my mapping file rather than the IUserType implementation.