Finalmente ho trovato la soluzione, hibernate e' davvero documentato male, in giro per la rete si trovano centinaia di documenti diversi, che si riferiscono alle piu disparate versioni e ognuno spiega qualcosa di diverso dall'altro o che negli altri non c'e'.
Grazie a uno di questi documenti ho risolto il mio problema, il documento dice:
codice:
Sometimes you want to use a view, but you cannot create one in the database (i.e. with a legacy schema). In this case, you can map an immutable and read-only entity to a given SQL subselect expression:
<class name="Summary">
<subselect>
select item.name, max(bid.amount), count(*)
from item
join bid on bid.item_id = item.id
group by item.name
</subselect>
<synchronize table="item"/>
<synchronize table="bid"/>
<id name="name"/>
...
</class>
Declare the tables to synchronize this entity with, ensuring that auto-flush happens correctly and that queries against the derived entity do not return stale data. The <subselect> is available both as an attribute and a nested mapping element.
Come potete vedere si riferisce a hibernate usando i file xml, e io odio i file xml.
Fortunatamente esiste l'annotazione corrispondente:
codice:
@Entity
@Subselect(value="select item.name, max(bid.amount) as m, count(*) as c "+
"from item "+
"join bid on bid.item_id = item.id "+
"group by item.name"
)
@Synchronize(value={"item ", "bid "})
public class Summary implements Serializable {
//...
}
dovete utilizzare gli alias come se fossero dei nomi di colonna e il gioco e' fatto.
Spero che la cosa possa essere di aiuto a qualcuno.
ps. chiederei a un moderatore di modificare il titolo