How can I export the hibernate.cfg.xml from the nhibernate configuration. I have configured nhibernate in code instead of importing the cfg.xml. I now have a need to export the cfg and the mapping xml files for import into other nhibernate tools. I found a way to export the mapping.xml but fail to find a method to export the cfg.
NHibernate 3.2
I doubt there is an easy way how to do it. NHibernate.Cfg.Configuration does not contains any reference to NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration class which conforms to nhibernate-configuration-2.2 xml schema. You could try to infer all properties of HibernateConfiguration class from Configuration instance by your own.
NHibernate.Cfg.Configuration itself is serializable but it is serialized into quite different schema.
IMO fallback to traditional cfg.xml file would be far easier solution.
Related
I wrote a jackson module to enable a specific type of serialization. Now i want to enable global configuration of one of the new serializers. so i have to set a property on a serializer instance during creation.
Is there a way i can do that from within a jackson module?
Module interface is stateless, one-of-thing, so it does not have default wiring to affect things it adds.
But what you can do is to use a work-around; possibilities include:
use of ThreadLocal; set before serialization, read from serializer
use new (Jackson 2.3) feature of "attributes"; can set those for writing (ObjectWriter.setAttribute()) and reading (ObjectReader.setAttribute()), accessible by serializer/deserializer through context object (SerializerProvider / DeserializationContext)
So hopefully one of these works for your use case.
Automapping in Nhibernate has been this wonderful magical thing, but now something strange is happening and I want to peel back this magical layer and see the actual mappings that are getting generated.
Is there a way to see the mappings generated by automapper and my overrides so I can see if it's doing what I think it's doing?
If you are using Fluent then you should be able to write the persistence model to the disk:
// In your fluent config code
// assuming config is of type FluentNHibernate.Cfg.FluentConfiguration
var model = new FluentNHibernate.PersistenceModel();
config.Mappings(m => m.UsePersistenceModel(model));
model.WriteMappingsTo(#"C:\some_folder_name_for_hbm_files_to_go_into");
in my project I need a class which contains the project configuration.
The configuration must be loaded from a XML file and must be a singleton.
In Guice there is a singleton scope. Now I have to "overwrite" the singleton with the deserialized configuration.
Is this somehow possible?
Important: pelase do note that Guice was originally created to get rid of all these huge and ugly XML files used by another DI library for managing dependencies. In general, when using Guice, you should be able to -almost- completely remove any XML from your project.
But if you must, perhaps because the XML file is generated by something outside your control then consider these:
Keep your whole configuration object and create a Provider for it, and bind it in Singleton scope. But you'll have to perform deserialization by yourself.
Or if your configuration is simply made of (name, value) pairs, then you can use java.util.Properties whcih can be loaded from an XML file, then use Guice Names.bindProperties() API in one of your Modules.
Then you can directly inject each single property by using #Inject #Named.
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.
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.