Problem loading only required fields using Resultsettransformer in Hibernate

Asked

Viewed 43 times

1

I’m trying to develop a method for through a SQL pure, seek only the fields necessary to mine query and finally, already bring the already mapped object.

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

So I’d like to know how to do the query and the method to popular the id of PropostaCartao and the id of Filial.

Thank you

  • 1

    How are you doing? Include the code that can reproduce your problem.

  • Dude it turned out I implemented a interface ResultTransformer and so passed the fields to the object.

  • 1

    @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 :)

  • 1

    @Dherik sorry, it’s lack of time. But I will post

  • @Dherik, it took me a while but I did it! VLW

1 answer

0

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.

Browser other questions tagged

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