1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Hibernate SQL Query result Mapping/Convert TO Object/Class/Bean

Discussão em 'Outras Linguagens' iniciado por Stack, Novembro 6, 2024 às 08:52.

  1. Stack

    Stack Membro Participativo

    1 2: select (table.*)/(all column) is OK

    String sql = "select t_student.* from t_student";
    //String sql = "select t_student.id,t_student.name,... from t_student"; //select all column
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Student.class);//or query.addEntity("alias", Student.class);
    //query.list();[Student@..., Student@..., Student@...]
    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); //or other transformer
    query.list(); //[{Student(or alias)=Student@...},{Student=Student@...}]


    3: select some column(not all of), is Error

    String sql = "select t_student.id,t_student.name.t_student.sex from t_student";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Student.class);
    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    query.list(); //Exception:invalid column/no column


    I want "3" to work ok, and let the result can be mapped to Student.class.
    Like: Student[id=?, name=?, sex=?, (other field are null/default)]
    I've no idea for this error, help me please!

    Continue reading...

Compartilhe esta Página