NHibernate and "anonymous" entities - nhibernate

I have these entities:
public class Parent
{
public int Foo { get; set; }
public Child C { get; set; }
}
public class Child
{
public string Name { get; set; }
}
I have query which fetches all Parent entities from the database. Then I keep them in memory, and filter them using LINQ queries.
I have noticed that when I do the DB query, NH selects all the Parent entities in one query (and of course fills the Foo property), and for each Parent I access with LINQ, NH fetches the infos of each Child.
How can I do to fetch all infos I need in one unique DB, and use the data with LINQ without it to generate additional DB trips?
Should I use the AliasToBeanResultTransformer? If so, must I create a DTO which will store the infos, like:
public class ParentDTO
{
public int Foo { get; set; }
public string ChildName { get; set; }
}
or must I still use the Parent class?
Thanks in advance

You can eagerly load the children for this query like this (using QueryOver syntax)
public IList<Parent> FindAllParentsWithChildren()
{
ISession s = // Get session
return s.QueryOver<Parent>()
.Fetch(p => p.C).Eager
.List<Parent>();
}
An alternative is to change your HBM files to indicate that Child is eagerly loaded by default. Then you won't need to alter your query.

You need to tell NHibernate not to use lazy loading for the relationship between the Parent and Child entities.

Related

Map two classes to the same table

There is a NODES table with dozen of 'small' columns and a LOB column in a legacy DB. A NodeEntity class is mapped to the NODES table.
For performance purposes I do not want to load LOB column every time I access the DB. I know two approaches to achieve this:
Lazy loaded properties
Separate entity class (the idea is taken from here)
Lazy loaded properties are good when you only loading data from DB. But if you have to save entities then there is a risk to lose your data if you forget to fetch lazy loaded properties beforehand.
So I chose the second approach.
I created separate small NodeEntityLite class with properties mapped to non-LOB columns of NODES table. I modified NodeEntity class so it inherits from NodeEntityLite class. I changed the mappings for my classes and used union-subclass for inheritance.
public class NodeEntityLite {
public virtual long Id { get; set; }
public virtual string Code { get; set; }
}
public class NodeEntity : NodeEntityLite {
public virtual string NOTE { get; set; } // type:clob
}
FluentNHibernate mapping for NodeEntityLite class is
public void Override(AutoMapping<NodeEntityLite> mapping) {
mapping.Table("NODES");
mapping.UseUnionSubclassForInheritanceMapping();
}
FluentNHibernate mapping for NodeEntity class is
public void Override(AutoMapping<NodeEntity> mapping) {
mapping.Table("NODES");
mapping.Map(e => e.NOTE).CustomType("StringClob").CustomSqlType("NCLOB");
}
I expected that when I execute select n from NodeEntityLite n where n.Id = :p0 HQL then NHibernate generates SQL commands without NOTE column:
select nodeentity0_.ID as id1_87_,
nodeentity0_.CODE as code2_87_
from from NODES nodeentity0_
where nodeentity0_.ID=:p0;
But NHibernate generates absolutely different SQL command (NOTE column is not skipped as I expected):
select nodeentity0_.ID as id1_87_,
nodeentity0_.CODE as code2_87_,
nodeentity0_.NOTE as note14_87_,
nodeentity0_.clazz_ as clazz_
from ( select ID, CODE, NOTE, 1 as clazz_ from NODES ) nodeentity0_
where nodeentity0_.ID=:p0;
I tried to change inheritance and to use other mappings but without success.
The question is: Can I map several classes to the same table in NHibernate to get access to different columns?
If yes, please give an example.
The solution (based on the suggestions from David Osborne and mxmissile) is not to use inheritance. I use common interface implementation instead of class inheritance. The working code is below:
public interface INodeLite {
long Id { get; set; }
string Code { get; set; }
}
public class NodeEntityLite : INodeLite {
public virtual long Id { get; set; }
public virtual string Code { get; set; }
}
public class NodeEntity : INodeLite {
public virtual long Id { get; set; }
public virtual string Code { get; set; }
public virtual string NOTE { get; set; } // type:clob
}
...
public void Override(AutoMapping<NodeEntityLite> mapping) {
mapping.Table("NODES");
}
...
public void Override(AutoMapping<NodeEntity> mapping) {
mapping.Table("NODES");
mapping.Map(e => e.NOTE).CustomType("StringClob").CustomSqlType("NCLOB");
}
Regardless of the inheritance, NH can map different types to the same table. I have done it, albeit without inheritance.
You should be able to remove this line from the NodeEntityLite override and achieve it:
mapping.UseUnionSubclassForInheritanceMapping();
If this proves unsuccessful, you might need to tune the automapping further. It's definitely possible though.

How to add many-to-many relation using stateless session in NHibernate?

I have two entities mapped to DB using NHibernate:
class Entity1
{
public int Id { get; set; }
public Entity2[] ReferencedEntities { get; set; }
}
class Entity2
{
public int Id { get; set; }
}
For Entity1 I also specify many-to-many relation to Entity2:
HasManyToMany(x => x.ReferencedEntities);
As I understand, internally NHibernate represents many-to-many relation creating some relation entity like:
class Reference
{
public Entity1 Entity1 { get; set; }
public Entity2 Entity2 { get; set; }
}
I'm adding those entities to DB using NHibernate stateless session like this:
using (var session = sessionFactory.OpenStatelessSession())
{
session.Insert(entity1);
foreach (var entity2 in entity1.ReferencedEntities)
{
session.Insert(entity2);
}
}
But I also want to add relation between them. For this, I need to save relation entity as well. How can I add many-to-many relation using stateless session? Do I need to specify relation entity implicitly or there is some another way?
Stateless session doesnt cascade operations so it wont save changes and links to the arrayelements if they are performed in other tables.
Unnecessary selects are often a sign of missing/wrong code like UnsavedValue() or Equals()``GetHashCode()

nHibernate Criteria for selecting all entities with empty child collections

I'm having difficulty writing a Criteria to select all entities with empty child collections or empty grand-child collections. I can do these as separate Criteria but I'm having trouble combining into a single Criteria.
class structure:
public class Component
{
public IList<Version> Versions { get; set; }
}
public class Version
{
public IList<SubscribeEvent> SubscribedEvents { get; set; }
public IList<PublishEvent> PublishedEvent { get; set; }
}
This does not work:
return session
.CreateCriteria<Component>("c")
.CreateCriteria("Versions", "v")
.Add(Restrictions.Or(Restrictions.IsEmpty("c.Versions"), Restrictions.And(Restrictions.IsEmpty("v.PublishedEvents"),
Restrictions.IsEmpty("v.SubscribedEvents"))))
.SetCacheable(true);
I've managed to work out a solution, not sure if it is the best (I should buy NH profiler)
return session
.CreateCriteria<Component>("c")
.CreateAlias("Versions", "v", JoinType.LeftOuterJoin)
.Add(Restrictions.Or(Restrictions.IsEmpty("c.Versions"),
Restrictions.And(Restrictions.IsEmpty("v.SubscribedEvents"),
Restrictions.IsEmpty("v.PublishedEvents"))))
.SetCacheable(true);

How can I sort in (n)hibernate on a property of a child object?

I have an object from my domain model that has a child object. How can I use a criteria query to order based on a property of the child?
For example:
class FooType
{
public int Id { get; set; }
public string Name { get; set; }
public BarType Bar { get; set; }
}
class BarType
{
public int Id { get; set; }
public string Color { get; set; }
}
...
// WORKS GREAT
var orderedByName = _session.CreateCriteria<FooType>().AddOrder(Order.Asc("Name")).List();
// THROWS "could not resolve property: Bar.Color of: FooType"
var orderedByColor = _session.CreateCriteria<FooType>().AddOrder(Order.Asc("Bar.Color")).List();
What do I need to do to enable this scenario? I'm using NHibernate 2.1. Thanks!
You need to either add an alias or create a nested criteria for your child. Not sure how to do this in NHibernate, in Hibernate it's done via createCriteria() and createAlias() methods.
You would then use the alias as prefix in order by.
Update Hibernate code sample:
Criteria criteria = session.createCriteria(FooType.class);
criteria.createAlias("bar", "b");
criteria.addOrder(Order.asc("b.color"));
I imagine in NHibernate it would be quite similar, though with property/entity names uppercased. Here's an example from NHibernate documentation.

Why does NHibernate first insert and then update element of a collection?

I'm not sure if it's a correct behavior or something done wrong on my side:
I've got a very simple parent-child relationship
public class SubmittedBatch
{
public virtual Guid Id { get; set; }
public virtual IList<SubmittedBatchParameter> Parameters { get; private set; }
}
public class SubmittedBatchParameter
{
public virtual string Value { get; set; }
}
And with FluentNH it is configured like:
mapping.HasMany<SubmittedBatchParameter>(sb => sb.Parameters).Cascade.All();
I do something easy like adding a new element to the Parameters collection and then call SaveOrUpdate on the parent element.
Looking at the trace of the SQL Statements I get an insert:
INSERT INTO [SubmittedBatchParameter]
(Value)
VALUES ('Disabled' /* #p0 */)
select SCOPE_IDENTITY()
and then an update:
UPDATE [SubmittedBatchParameter]
SET SubmittedBatch_id = '209971b7-c311-46bd-b989-9cf80113654c' /* #p0_0 */
WHERE Id = 39 /* #p1_0 */
Why isn't NH just doing the Insert with also the Guid specified?
Is this correct, or am I doing something wrong?
Thank you
Simone
You have not mapped the reverse parent relationship explicitly. Therefore the only way that NHibernate knows to save the SubmittedBatch_id column value is when you save the parent object. It can't save both objects at once, so what it does is to save the child, then when it saves the parent, save the relationship.
In fact, even if you were to map both directions of the relationship, you would still have to specify which is the "master" by marking the other as an "inverse" relationship. The field is then updated by saving the master side.
So, if you were to map a SubmittedBatch property in the SubmittedBatchParameter class, map this as the "master" (i.e. set the collection mapping as the inverse using .Inverse in Fluent), and then set that when you add the parameter to the batch, then you would see just the insert that you expect.
Here's what I mean:
public class SubmittedBatch
{
public virtual Guid Id { get; set; }
public virtual IList<SubmittedBatchParameter> Parameters { get; private set; }
}
public class SubmittedBatchParameter
{
public virtual SubmittedBatch SubmittedBatch { get; set; }
public virtual string Value { get; set; }
}
Then in child mapping:
HasMany<SubmittedBatchParameter>(sb => sb.Parameters).Inverse();
and in parent mapping:
References(sbp => sbp.SubmittedBatch);