consider table
sales (id, seller_id, amount, date)
and here is a view that is generated from sales using query SELECT seller_id, SUM(amount) FROM sales GROUP BY seller_id
total_sales (seller_id, amount)
I want to make an entity for total sales but without the view on the sql side.
This entity will be constructed from a query. The closest thing I found is this, but I could not make it work.
Even if I define the loader, hibernate looks for the entity’s table and gives an error if it cannot find it. If I create the table it does not load the entity from the named query I defined, Hibernate generates the query itself.
Is there a way to make @Loader to work or is there another way that I can map a query to entity?
Why don’t you just use
newin the query?You just write a class TotalSales with a constructor taking the seller_id and an integer.
Edit: When using criteria API, you can use the
AliasToBeanResultTransformer(See API docs). It copies every alias name to a property of the same name.Then your
TotalSalesneeds aSellerIdandCountproperty.