What namespace do I need to have in my project to execute the command;
ISession.Evict(obj)????
You should just need NHibernate and whatever you're using for proxy like NHibernate.ByteCode.Castle. Should have no problem with ISession with just those.
Note also that you'll need FluentNHibernate if you are using any of Fluent's items in the same class.
Related
I am considering using Fluent NHibernate for my project and I haven't found any documentation on whether FH supports NHibernate settings such as show_sql and prepare_sql. I could live without show_sql in a pinch, but prepare_sql is important for ensuring good performance at run time.
Can anyone tell me if it's possible to configure these settings in Fluent NHibernate?
Yes, you can.
Fluently.Configure()
.Database(ConfigureDatabase())
.Mappings(ConfigureMapping)
.ExposeConfiguration(ModifyConfiguration)
.BuildConfiguration();
And now in ModifyConfiguration method you have plain NHibernate's Configuration object to modify
private void ModifyConfiguration(Configuration configuration)
{
// set parameters here like this:
configuration.Properties["show_sql"] = "true";
}
Some of the settings are exposed through the fluent API.
See here for examples: Database Configuration
Anything that isn't supported through specific fluent calls can be set by manipulating the native NHibernate.Cfg.Configuration object. Either way you can do everything in code that you can with the configuration file.
I want to use fluent-nhibernate to query my data, but when a entity gets saved, it should not be written into the database via insert/update. Instead, I want to (better: have to) serialize that object and send it to a webservice (which will map that object to a 3rd-party class that will trigger some important business-logic).
Is such behaviour possible to implement with nhibernate (call a custom method instead of update on saving)?
I would recommend creating a IRepository interface and hiding the webservice and Nhibernate functionality behind that. You could possibly use NHibernate interceptors for this, but it doesn't sound like a clean solution. Personally, I would hate to find Web service code hidden in one of Nhibernate interceptors.
We decided to use a SaveOrUpdateEventListener for this task.
I'm somewhat new to the MVC framework and in accordance with the following post:
NHibernate with StructureMap I am not sure how to actually get the HttpContextScoped ISession in my controller?
I'm sure there is a simple way to do this but I am unsure.
Also, it's a small project and I don't want to go overboard with Enterprise Design Patterns.
Thanks!
did you try writting this line in your action method of your controller ?
ISession session = ObjectFactory.GetInstance<ISession>();
What is Castle proxy factory in NHibernate? What is its task? What does proxy mean in this case?
Castle can be used (amongst others, you have the choice, you can also use LinFu, Spring.NET, ...) to create dynamic proxies of your entities.
By default, NHibernate uses dynamic proxies to represent your entities; by doing so, it is able to return an object to you when you retrieve some entity from the DB, without all properties being populated. By using a dynamic proxy, it will only populate the entity once you really refer to a property.
(So it is some kind of lazy loading; not to be confused with lazy loading of collections / associations though).
This behaviour is the reason why NHibernate wants you to create every property as virtual by default: NHibernate will create a new class using this Castle (or LinFu, ...) proxy provider which inherits from your entity, and it will override all the properties so that it can 'inject' the code that is necessary to retrieve the necessary data from the DB.
You can disable this behaviour by specifying 'lazy=false' in your entity mapping. (Although, I do think that even if you disable this feature, NHibernate will still require that you use one of the proxy factories).
When you are selecting an entity from ISession you are getting not real entity instance - you are getting proxy object.
This proxy object inherits your entity and used by NHibernate to track changes made to the fields.
see it: http://en.wikipedia.org/wiki/Proxy_pattern
I am trying to get test running that uses FluentNHibernate to map my
entities.
I get unknown entity class error.
I have a Domain project that has my entities and the mappings in a seperate
folder in that project.
My test project has the Nhibrenate configuration in the App.Config.
Any ideas please???
Malcolm
EDIT: I have this method that creats the ISessionFactory but I dont think this code is working and this is why it does not recoginze me mappings exist at all. The SessionFacoryCreator is the assembly with my AppConfig and Recipe is my assembly where the Domain objects are. Please HELLLLLLP!!!!
public static ISessionFactory Create()
{
Configuration cfg = new Configuration()
.AddAssembly(typeof(SessionFactoryCreator).Assembly)
.AddAssembly(typeof(Recipe).Assembly);
return cfg.BuildSessionFactory();
}
Have you read the wiki? Your example shows nothing of hooking Fluent NHibernate into the session factory building, that'd be why it isn't working. Start with the Getting Started guide.
How do you add your Entities to the Hibernate framework? Are you using AnnotationConfiguration or just Configuration? If you are using annotations you must add your annotated entity to the framwork. Otherwise, if you are using xml hibernate mapping, the syntax could be worng (iirc)
NHibernate and I keep on getting the following error: MappingException: Unkown Entity Class
If you get MappingException error, there are two things you should check out:
Make sure the name of the *.hbm.xml is spelled correctly — not *.hmb.xml nor *.mbh.xml
Make sure the Build Action property is set to “Embeded Resource.”