nhibernate

Asynchronous queries in a web app, using NHibernate

In a web application, the Session is only available in the current thread. Does anyone have any tips for executing queries through NHibernate in a new asynchronous thread? For example, how could I make something like this work: public void Page_Load() { ThreadPool.QueueUserWorkItem(state => FooBarRepository.Save(new FooBar())); } ...

How to avoid NHibernate.NonUniqueObjectException

I'm writing a blog engine as a learning exercise. I know there are plenty of blog engines out there, but bear with me... I have a BlogPost entity that has a property Tags that is an IList of tags associated with it. The BlogPost.SetTags(string) method splits the string, creates new Tag objects with the specified tag name, and adds the...

Multi table COUNT in NHibernate with joined subclasses

I have been trying to find a way to do this query using a NHibernate criteria (preferred) or HQL, with no luck. Here is the query: select COUNT(sa.Id) from Accounts a join Sources s on a.Id = s.Account_Id join SpecialArticles sa on sa.SpecialSource_Id = s.Id Notes: SpecialSource is subclass of Source and is mapped as a JoinedSubCla...

What are the upsides and downsides of NHibernate?

Hi, For a new project we are looking at NHibernate. We like it a lot overall, but one thing bothers us: it seems to be very resource consuming. Apparently NHibernate will load all the properties of an object even if you need only one of the properties. More over, it will do the same for the properties of child objects. So we are weighi...

NHibernate not doing cascading saves when auto-dirty-check is disabled and object graphs are loaded manually

It's a long shot that anyone will have dealt with this, but here goes. First, I have NHibernate's auto-dirty-check behavior disabled. I did this because I don't want NHibernate to save every changed object that it knows about when I commit a transaction (FlushMode = Commit) because it get a little over-zealous sometimes and tries to sa...

Can't get log4net to output anything when using NHibernate

I have this in my web.config file (edited to reflect some changes): <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net debug="true"> <appender name="DebugAppender" type="log4net.Appender.AspNetTraceAppender"> <layout type="log4net.L...

Why is NHibernate doing an UPDATE rather than an INSERT?

Continuing my explorations in NHibernate with my podcast application, I've come across something odd: NHibernate is UPDATEing when I would expect an INSERT. The situation is a simple one-to-many relationship between a podcast and its items. Here's my NHibernate mapping: <hibernate-mapping assembly="App.DataModel"> <class name="Feed"...

NHibernate Lambda Extensions - Eager Loading a collection's assosciations

I've got a Criteria Query for a social networking site. A Person object has a collection of Friends (also person objects). The query grabs the first N friends, but I also want to eager load an associated object MainProfileImage and then a subsequent associated object MediumThumbnail. I can do this in HQL easily: select friends from P...

NHibernate: Remove an Item From a Persisted Collection Automatically

Using NHibernate, I am looking for a way to automatically update persisted collections when a persisted item is deleted. For instance: var post = GetNewPost(); var blog = GetCurrentBlog(); blog.Posts.Add(post); BlogRepository.Update(blog); User.Posts.Add(post); UserRepository.Update(user); ---- // Somewhere else var blog = GetCurrent...

Need help for complex SQL query in NHibernate

I need to know how I can rewrite the following SQL query in NHibernate's ICriteria format. It is basically a way to mimic the MS-SQL's RANK() feature and return only those results that are most current. SELECT a.Name, a.Value, a.CreationDate FROM MyTable a WHERE EXISTS ( SELECT NULL FROM ( SELECT TOP 1 CreationDate FROM M...

How do I map an insert only table with NHibernate?

Hi, I have a scenario where I have a parent class with some definitions we can call if Foo to be unique and then a child class that I chose to call Bar. Together they make Foo Bar! :) public class Foo { public Foo() { Bars = new List<Bar>(); } // Internal id only, should probably not even map it public virtua...

NHibernate Select in From Case

Hello I have problems with a query in NHibernate. The original SQL query looks like SELECT Id ,Table1_Id ,Table2_Id ,Table3_Id FROM ( SELECT Id ,Table1_Id ,Table2_Id ,Table3_Id FROM Table_123 WHERE Table2_Id = 72 UNION SELECT 100 As Id ,151 As Table1_Id ,72 As Table2_Id ,2...

How best to retrieve and update these objects in NHibernate?

Hi, I previously asked a question regarding modeling of a situation with Users, Items, and UserRatings. In my example UserRatings are associated with one User and one Item. A good answer was provided by Nathan Fisher and I've included the model he suggested below. But I now have a question regarding retrieval of these objects. The...

Fluent NHibernate inheritance mapping problem

Hello, I am using Fluent NHibernate with table per subclass inheritance mapping. I want to reference to a list of specific objects, but i can't figure out, how to restict the result to objects of one specific class. class PetMap : ClassMap<Pet> { public PetMap() { Id(c => c.ID).GeneratedBy.Identity(); } ...

Compare only Date in nhibernate linq on a DateTime value

Hello, I trying to compare two dates (DateTime) in nhibernate linq: query = query.Where(l => (l.datCriacao.Date == dtLote.Date) but the error: NHibernate.QueryException: could not resolve property: datCriacao.Date of: SAGP.Entities.Lote anyone knows how I can solve this? tks ...

NHibernate poor performance on auto flush events?

Let me start by saying that I know that NH doesn't recommend using bulk operations. But as a bit of interest, I was wondering why this is so expensive. For 200 objects in my system, it takes 4 minutes, for which 90+% of the time is spent in DefaultAutoFlushEventListener.OnAutoFlush (Thanks to RedGate Profiler). That's insane. I wonde...

NHibernate Versioning

I'd like to do something like the acts_as_versioned Rails plugin in NHibernate. Anyone have any suggestions for how to do this? Do I have to create a class for Entity and VersionedEntity? I'm pretty sure you'd use an Interceptor to write to the version table, but are there any suggestions for doing this? Thanks, Nathan ...

Why is NHibernate AutoFlush check so expensive?

In practice we are finding the default NHibernate (v2.0 & 2.1) FlushMode=Auto to be extremely expensive. Reviewing the NHibernate source suggests that the algorithms for determining what needs to be flushed rely on brute-force of looping through all entities in session, and this occurs for every query run in a transaction. In some produ...

NHibernate criteria datetime format issue

Hi I have a simple nHibernate criteria query which is looking for records with a datetime less than today: example: criteria.Add(Expression.Le("TheDate", DateTime.Today)); However, the results I am getting are incorrect when the day/month is ambiguous (eg 12th November 2009 returns the records for 11th December 2009) I have looked ...

NHibernate IDictionary mapping with link table

I am a newbie to NHibernate and trying to create a XML mapping for this scenario: public class Catalog { public virtual Guid ID { get; set; } public virtual string Name { get; set; } public virtual IDictionary<string, Product> Products { get; set; } } The key in the IDictionary is the name of the Product. public class Pro...