Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Nhibernate inner join

I have 3 tables (Master, Imagen, Linea) were:

Master
public virtual int Id { get; private set; }
public virtual Imagen imagen { get; set; }

Imagen
public virtual int Id { get; private set; }
public virtual Linea linea { get; set; }

Linea
public virtual int Id { get; private set; }
public virtual String Nombre { get; set; }

I need a query like this:

SELECT * FROM dbo.Master 
INNER JOIN dbo.Imagen ON dbo.Master.imagen_id = dbo.Imagen.Id 
INNER JOIN dbo.Linea ON dbo.Imagen.linea_id = dbo.Linea.Id 
WHERE dbo.Linea_Id = 5

But I dont know how to tell Fluent Nhibernate to create this query using the automapper. So far I've tried this:

ICriteria c = session.CreateCriteria(typeof(Master))
  .CreateAlias("dbo.Imagen", "img", JoinType.InnerJoin)
  .Add(Restrictions.Eq("img.linea_id", id_linea));

return c.List<Master>();

But I get this error: could not resolve property: dbo of: ImageManager.Model.Entity.Master

Any ideas on how to do a Inner Join? Thanks in advance

like image 640
lloiacono Avatar asked Oct 27 '11 13:10

lloiacono


1 Answers

For a start I would get rid of the dbo from dbo.Imagen. Using the ICriteria interface you need to think in-terms of objects, not database tables, even though there may be a one to one mapping of object to table and properties to columns.

EDIT:
another option would be to use the QueryOver Lambda syntax.

   var list = session.QueryOver<Master>()
                        .JoinQueryOver(master => master.imagen)
                        .Where(imagen => imagen.linea.Id == 5)
                        .List();
like image 196
Nathan Fisher Avatar answered Sep 21 '22 02:09

Nathan Fisher