0

i need to build a sql query that returns a bean with inside an entity... i try to explain me with an example.

This is my bean that i want to get from the query:

public class EventiPerGiorno { 

    private Eventi evento; // THIS IS AN ENTITY
    private Date primoSpettacolo; //SCALAR
    private int spettacoli; //SCALAR
...
}

and this is the xml query mapping:

<sql-query name="eventiPerGiorno">
    <return alias="evento" class="entity.Eventi"/>
    <return-scalar column="spettacoli" type="integer"/>
    <return-scalar column="primoSpettacolo" type="date"/>
    <![CDATA[
        select 
            spettacoli.evento as {evento.id},
            count(spettacoli.id) as spettacoli,
            min(spettacoli.inizio) as primoSpettacolo,
        from ...
    ]]>
</sql-query>

But Hibernate says to me that it want ALL properties of Eventi in the select not only the primary key that is "evento.id". Why?

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • Because you told it to return instances of Eventi. So all the properties of Eventi are needed to poulate these returned instances. – JB Nizet May 18 '12 at 10:27
  • Can't it automatically get all property from db or cache? – Tobia May 18 '12 at 10:31
  • 1
    The point of the query is precisely to make it get the properties from the DB. It would be very unefficient for the query to return 1000 IDs, and for Hibernate to be forced to execute 1000 queries to get the properties of these 1000 eventi. – JB Nizet May 18 '12 at 10:42
  • Of course you're right. I'm working with something already catched... but I agree with you. – Tobia May 18 '12 at 10:51
  • Then make the query return the IDs instead of the entities, and execute Session.get() explicitely, and the entity will be returned from the cache. – JB Nizet May 18 '12 at 10:53
  • I could... but i already follow your answer :-) – Tobia May 18 '12 at 11:45

0 Answers0