Alex Rivera | Logout

Fluent NHibernate fetching view without unique identifier

Asked 2010-05-27T10:44:45.677
12

I'm trying to map a view without an identifier, but nhibernate still generates a sql with the id column (giving me a sql error, since the ID column does not exists in the db). Maybe I'm misunderstanding the Id() constructor?

constructor comments:

Create an Id that doesn't have a corresponding property in the domain object, or a column in the database. This is mainly for use with read-only access and/or views. Defaults to an int identity with an "increment" generator.

public class PersonMapping : ClassMap<Person>
{
    public PersonMapping()
    {
        Table("person");
        ReadOnly();

        Id();
        Map(f => f.Name, "name");
    }
}
Edit
Report

1 Answer

7

You could retrieve the records as value objects (non-managed entities) instead of entities.

"14.1.5. Returning non-managed entities

It is possible to apply an IResultTransformer to native sql queries. Allowing it to e.g. return non-managed entities.

sess.CreateSQLQuery("SELECT NAME, BIRTHDATE FROM CATS")
    .SetResultTransformer(Transformers.AliasToBean(typeof(CatDTO)))

This query specified:

  • the SQL query string
  • a result transformer

The above query will return a list of CatDTO which has been instantiated and injected the values of NAME and BIRTHNAME into its corresponding properties or fields. "

answered 2010-11-18T09:53:23.803

Your Answer