Get a list of mapped data in Nhibernate

Asked

Viewed 110 times

2

I am trying to transform the data returned from the database to the settings made in my mapping, however I get the following criticism:

"The value "System.Object[]" is not "Domain.Classes.Classeemplo" and cannot be used in this generic collection."

I’ve tried using the "Transformers.Aliastoben", however I realized that ignores all my mapping. I am still very new in this area, if anyone can help me I thank you.

My Class:

public class ClasseExemplo
{
    public virtual int ID { get; set; }
    public virtual string Descricao { get; set; }
    public virtual Usuario Usuario { get; set; }
    public virtual DateTime TimeStamp { get; set; }
}

Mapping:

    public ClasseExemploMap()
    {
        Id(x => x.ID);
        Map(x => x.Descricao);
        Refereces(x => x.Usuario).Column("USUARIO_ID").Cascade.None();
        Map(x => x.TimeStamp);

        Table("TABELA");
    }

Repository:

public IList<ClasseExemplo> BuscarLista(int usuario)
{
    string SQL = "select * " +
     "from MinhaTabela " +
     "where Usuario_ID = :id";

    return Session.CreateSQLQuery(SQL)
           .SetParameter("id", usuario)
           //.SetResultTransformer(Transformers.AliasToBen(typeof(ClasseExemplo))
           .List<ClasseExemplo>();
}
  • Don’t have the User_id field? At least in your class and map code

  • Thank you for the reply Virgilio, I had forgotten to mention the Column on my mapper, but the error still persists.

  • How is this table and a test takes that generic and checks the return with List ()

  • Can not understand very well, I really need to return the values molded to my class, it is not possible to return the values only with List().

1 answer

1


Yes, I ended up doing a test and it works perfectly, I think it’s doing something wrong, an example to illustrate:

Class

public class Credit
{
    public virtual int Id { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime? Created { get; set; }
}

Mapclass

public class CreditMap: ClassMap<Credit>
{
    public CreditMap()
    {
        Table("credit");

        Id(x => x.Id)
            .Column("id")
            .GeneratedBy.Sequence("credit_id_seq");

        Map(x => x.Description)
            .Column("description")
            .Length(100)
            .Not.Nullable();

        Map(x => x.Created)
            .Column("created")
            .Nullable();
    }
}

Now to search a id that will bring only one item:

int id = 1;
var creditFindId = connection
            .CreateSqlQuery("SELECT * FROM credit WHERE id=:id")            
            .SetResultTransformer(Transformers.AliasToBean<Credit>())
            .SetParameter("id", id)
            .UniqueResult<Credit>(); // Resultado único

or to filter by a specific field to bring an item list:

int id = 1;
var creditFindId = connection
    .CreateSqlQuery("SELECT * FROM credit WHERE id=:id")
    .SetResultTransformer(Transformers.AliasToBean<Credit>())
    .SetParameter("id", id)
    .List<Credit>(); // Lista

because that kind of method of NHibernate return the data in a object array (object[]) and not in a strongly typed type, for that you need to use:

  • .SetResultTransformer(Transformers.AliasToBean<Credit>()) to know what kind of data should be processed
  • .UniqueResult<Credit>() or .List<Credit>() for the specific type forcing the previous configuration.

If it is to make filters and already treat the data with the specific type without using SQL can be done as well:

int id = 1;
var item1 = connection.QueryOver<Credit>()
     .And(x => x.Id == id) // filtro
     .SingleOrDefault<Credit>(); // Resultado único

int id = 1;
var list1 = connection.QueryOver<Credit>()
     .And(x => x.Id == id) // filtro
     .List<Credit>(); // Lista

Observing: where it is written //filtro can be placed other as many filters and ordering.

Remember that if the result is by the primary key you can do so:

int id = 1;
Credit credit = session.Get<Credit>(id);

and so on, it’s not too difficult once you take the methods and their functionality is quiet handling.

  • 1

    Thank you very much I managed to solve the problem, for some reason I can not return the list with the Createsqlquery, I used the example of Queryover as a basis and succeeded in what I wanted, thank you very much :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.