background preloader

Nhibernate

Facebook Twitter

Smulo

Fluent. Attributes. NHibernate ISession Flush: Where and when to use it, and why. NHiberante: Querying Many To Many associations using the Criteria API. Over a year ago I was asked how we can query a many to many association with NHibernate using the criteria API. At the time, that was not possible, but the question came up again recently, and I decided to give it another try. First, let us recall the sample domain model: Blog m:n Users 1:m Posts n:m Categories 1:m Comments And what we want to do, which is to find all Posts where this condition is met: Blog.Users include 'josh' and Categories includes 'Nhibernate' and a Comment.Author = 'ayende'.

At the time, it wasn't possible to express this query using the criteria API, although you could do this with HQL. I did mention that a year have passed, right? Now it is possible, and easy, to do this using the criteria API. And this produces the following SQL: I am pretty sure that this is already in 1.2, but I don't have that handy to check. Databases supported by NHibernate.

Creating a custom id generator for nHibernate - NHibernate Forge. I originally blogged about this here and here on my blog but Fabio suggested I add these posts to the Forge, so here I am. I have to say I am a little exited to make my first contribution to the Wiki [H]. I will try to add a bit more to them and show some examples that do not use the TableGenerator class but directly implement IIdentifierGenerator .

Please let me know if there are any mistakes or anything else you want to see. Before beginning I recommend that you download the NH source code and take a quick look at the TableGenerator , IIdentifierGenerator and IPersistentIdentifierGenerator classes as you will be working with these classes and interfaces to implement your custom generator. Part 1: Inheriting from TableGenerator class Currently I am doing some work developing a utility that imports securities data into a fairly old legacy database. The original application for this database uses a primitive hilo-style ID generator mechanism.

So what to do? Return counter + SeedValue + 1; NHibernate and Inverse=True|False Attribute. In order to truly understand the inverse attribute in NHibernate, you need to first look at your relationship from the database table point of view and how your domain objects maps into their respective tables. It comes down to determining who is the owner of the relationship association between the parent and child objects. Suppose you have ParentTable (parentId PK, parentcol1, parentcol2) and ChildTable(childid PK, parentid FK, childcol1, childcol2). In a one-to-many relationship, you will have one parent and many children. The ChildTable has a primary key and foreign key.

Where does inverse come in? Many-to-one relationship The entity declaring the many-to-one relationship is the child object (or owner of the relationship), as its table has the foreign key, while the object that is referenced by the entity declaring the many-to-one relationship is the parent object. Hibernate conventions. Contextual data using NHibernate filters - NHibernate Forge. I'm in the middle of the development process of an application using NH for data access, and I'm faced with a requirement that could be stated as follows: The application needs to provide support for different Contexts of execution, and certain entities must be context-aware, which means that at a given time, the application only sees instances of these entities that correspond to the current context of execution.

Now, just remember that I have several entities defined that are used throught the entire application layer stack, so I wanted to solve this issue modifying as little as possible. I'm very proud with the solution that I came up with, and also very amazed by the power of NHibernate. To simplify a little lets assume that I have a static class that defines the current context of execution: public enum ContexType{ ContextA, ContextB,}public static class Context{ public static ContextType Current { get; set; }} public interface IcontextAware{ ContextType Context { get; set; }}