Problem loading only needed fields using ResultSetTransformer in Hibernate

1

I'm trying to develop a method for using a SQL pure, fetch just the required fields from my query and finally, already bring the object already mapped.

In case the object would be PropostaCartao and inside it would have another called Filial . I would like to know how to SQL to bring the id of the PropostaCartao and the id of the Filial and indicate that both are of type Long using the Scalar resource because the field is of type Long.

So, I'd like to know how to query and method to popular id PropostaCartao and id Filial .

Thank you

    
asked by anonymous 04.11.2015 / 15:39

1 answer

0

My goal is to make a query and popular the object with that return.

My challenge was because I do not know how to return the same object if it makes a query via objeto in Hibernate , since it has several objects associated with objeto main.

So I did that I could implement a Interface ResultTransformer which has two methods.

The transformTuple(Object[] tupla, String[] alias) method is where you associate objeto with the result of query .

The transformList(List lista) method returns lista .

What they need to understand is the following, in the first method informed above the following process occurs:

You will instantiate a new objeto , and each attribute of it will be popular, like this:

PropostaCartao propostaCartao = new PropostaCartao();
propostaCartao.setId(Long.valueOf(String.valueOf(tupla[0]))); 

SELECT campo_1, campo_2 FROM tabela;

At the time of turning you will do the following:

Objeto objeto = new Objeto();
objeto.setcampo1(tupla[0]);
objeto.setcampo2(tupla[1]);

And at the time of executing the query call the class created this way:

String sql = this.filtrarDados(filter);
Query query = HibernateSessionFactory.getSession()
              .createSQLQuery(sql)
              .setResultTransformer(new RelatorioExtratoLojistaAnaliticaResultTransformer());

In method setResultTransformer , you put your newly created class.

    
03.12.2015 / 12:36