I have a product table:
Product
- PId
- Name
ProductPricing
- PPId
- PId
- startDate
- endDate
- price
For one product, there can be multiple ProductPricing records.
For retrieving current price for the product, I use the following query:
SELECT PId, Name, price, startDate, endDate
FROM PRODUCT, PRODUCTPRICING
WHERE PRODUCT.PId = givenId AND PRODUCT.PId = PRODUCTPRICING.PId
AND (today() > PRODUCTPRICING.startDate AND today() < PRODUCTPRICING.endDate);
How do I map the result of this query to my POJO using Hibernate
??
my Java Product object looks something like this:
Product
- Id
- Name
- ProductPricing Object
ProductPricing
- startDate
- endDate
- price
This will give a slightly different query (with a sub-query instead of a join), but the result should be the same.
@Entity
class Product
{
@Id
int id;
String name;
@Formula("(select pp.price from PRODUCTPRICING pp where pp.PId = id and today() > pp.startDate AND today() < pp.endDate )")
double price;
}
If you want to map the ProductPricing as a POJO, too, I think you can't map it as a one-to-one, since it's technically a one-to-many with a filter which will hopefully result in only one result. So you'll have a Set<ProductPricing> in your Product class:
<set name="prices" table="PRODUCTPRICING">
<key column="PId"/>
<composite-element class="ProductPricing">
<property name="price"/>
<property name="startdata"/>
<property name="enddate"/>
</composite-element>
<filter condition="today() > startDate AND today() < endDate"/>
</set>
With composite-element, you don't need a primary key PPId in the PRODUCTPRICING table.