A
A
Alexiuscrow2015-04-05 17:05:52
Java
Alexiuscrow, 2015-04-05 17:05:52

Hibernete: How to create an Entity for SQL queries with mixed fields?

It is necessary to make a selection from the table. As a result, the values ​​of the fields of one table, the value of the 1st field of another table, and 1 calculated field should be obtained.
When sending a request, you must specify the entity class. But I don't have such an entity with such (additional fields) fields. There are only such entities, where each contains only those fields that are inherent in 1 specific table.
How to be in such a situation? What do you advise?

Query query = session.createSQLQuery("SELECT shops.id, localities.id, 
                                      COS(?) AS cosinus, 
                                      SIN(?) AS sinus 
                                      FROM shops, localities 
                                      WHERE localities.name = ? 
                                      AND localities.id = shops.locality_id;")
      		   .addEntity(ShopsPlus.class)
      		   .setString(0, a.toString())
      		   .setString(1, a.toString())
      		   .setString(2, cityName);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur, 2015-04-05
@timych

And in what object you were going to write down result of request? Some kind of POJO is still needed.
In general, look at QueryDsl
www.querydsl.com
and specifically at ConstructorExpression - a very handy thing (especially if you use maven)
www.querydsl.com/static/querydsl/3.2.2/apidocs/com...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question