Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

java - Get a list of bean type without being mapped in Hibernate

I don't know if this is even possible in hibernate.

Can I get a list of a bean type (which hasn't been mapped) with the getSession() of Hibernate?

What I'm trying to do is get two values from a query. Those values are from differents tables. I have done my query but when I try to execute the getSession, it says that my Class is not Mapped. I don't map it becasue there is no table to be mapped. I created a class named as ObjetivoAdapter which contains two Strings (The information i need to put into a list). Also I created the class because I need a list of my information.

Those are the tables from which I get the information:

Table: producto. Column: descripcion. Column: codigo_barras

Table: objetivo. Column: codigo_barras_objetivo (FK with codigo_barras from table producto). Column: promocion_id (FK with id from table promocion)

Table: promocion Column: id

This is my class bean ObjetivoAdapter:

public class ObjetivoAdapter {

    private String nombreProducto;
    private String codigoBarrasObjetivo;

    public void ObjetivoAdapter(String nombreProducto, String codigoBarrasObjetivo) {
        this.nombreProducto = nombreProducto;
        this.codigoBarrasObjetivo = codigoBarrasObjetivo;
    }

    public String getCodigoBarrasObjetivo() {
        return codigoBarrasObjetivo;
    }

    public void setCodigoBarrasObjetivo(String codigoBarrasObjetivo) {
        this.codigoBarrasObjetivo = codigoBarrasObjetivo;
    }

    public String getNombreProducto() {
        return nombreProducto;
    }

    public void setNombreProducto(String nombreProducto) {
        this.nombreProducto = nombreProducto;
    }

}

And this is my query:

public List<ObjetivoAdapter> getInfo(String codBarras) {
        String sql = "SELECT o.codigo_barras_objetivo, p.descripcion_corta FROM objetivo o "
                + "INNER JOIN producto p ON o.codigo_barras_objetivo = p.codigo_barras "
                + "INNER JOIN promocion pr ON o.promocion_id = pr.id "
                + "WHERE codigo_barras = ' " + codBarras + "';";
        List<ObjetivoAdapter> listaOA = getSession().createSQLQuery(sql).addEntity(ObjetivoAdapter.class).list();
        return listaOA;
    }

Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a setResultTransformer(AliasToBeanResultTransformer) instead of addEntity() and all should be fine


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...