My goal is this, 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 the Hibernate, because it has several objects associated with objeto leading.
So I did that I could implement a Interface ResultTransformer which has two methods.
The method transformTuple(Object[] tupla, String[] alias) is where the association of objeto to the outcome of query.
The method transformList(List lista) returns to lista.
What you need to understand is the following, in the first method reported above the following process occurs:
You will instantiate a objeto new, and popular every attribute of it, so:
PropostaCartao propostaCartao = new PropostaCartao();
propostaCartao.setId(Long.valueOf(String.valueOf(tupla[0])));
I inform that the order of the tuple object follows the order of the fields in the query, that is, if you do so:
SELECT campo_1, campo_2 FROM tabela;
When it’s time to turn you will do the following:
Objeto objeto = new Objeto();
objeto.setcampo1(tupla[0]);
objeto.setcampo2(tupla[1]);
And when executing the query call the created class this way:
String sql = this.filtrarDados(filter);
Query query = HibernateSessionFactory.getSession()
.createSQLQuery(sql)
.setResultTransformer(new RelatorioExtratoLojistaAnaliticaResultTransformer());
In the method setResultTransformer, you put your newly created class.
How are you doing? Include the code that can reproduce your problem.
– Bruno César
Dude it turned out I implemented a
interfaceResultTransformerand so passed the fields to the object.– Macario1983
@Macario1983, put a more complete answer, even if it is your own question, for the person who goes through the same doubt have more details of how you solved :)
– Dherik
@Dherik sorry, it’s lack of time. But I will post
– Macario1983
@Dherik, it took me a while but I did it! VLW
– Macario1983