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.
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:
With composite-element, you don’t need a primary key PPId in the PRODUCTPRICING table.