I have this mapping:
[Class(Table = "foo", Name = "Foo")]
public class Foo
{
[Id(0, TypeType = typeof(long), Name = "Id", Column = "id")]
[Generator(1, Class = "native")]
public virtual long Id { get; set; }
[ManyToOne(Class = "Bar", Column = "bar_id", NotNull = true)]
public virtual Bar Bar { get; set; }
}
[Class(Table = "bar", Name = "Bar")]
public class Bar
{
[Id(0, TypeType = typeof(long), Name = "Id", Column = "id")]
[Generator(1, Class = "native")]
public virtual long Id { get; set; }
[Bag(0, Inverse = true, Table = "foo", Cascade = "save-update")]
[Key(1, Column = "bar_id", ForeignKey = "fk_foo_bar_id")]
[OneToMany(2, Class = "Foo")]
public virtual IList<Foo> Foos { get; set; }
}
and I would like to do the SQL query:
SELECT bar_id FROM foo f WHERE f.id = 1
I know I can do
Session.Get<Foo>(1).Bar.Id however, it loads the Bar object and if it a very HEAVY object, my simple query, that only wants something that is inside the foo table is very slow. How should I do it ?
I thought about adding a property like this
[Property(Column = "bar_id", NotNull = true)]
public virtual long BarId { get; set; }
But I don’t know if it is wrong or not, or if I should it differently.
Finally we did it like this:
My conclusion is that NHibernate is cool, if you do only what they want you to do, but as soon as you have some performance issue and want to optimize the query, you need to come back to SQL and the pain begins.