如何使用hibernate将结果集从mysql映射到pojo?
我有一个产品表:
Product
- PId
- Name
ProductPricing
- PPId
- PId
- startDate
- endDate
- price
对于一种产品,可以有多个 ProductPricing 记录。
为了检索产品的当前价格,我使用以下查询:
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
??
我的 Java Product 对象看起来像这样:
Product
- Id
- Name
- ProductPricing Object
ProductPricing
- startDate
- endDate
- price
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将给出稍微不同的查询(使用子查询而不是联接),但结果应该是相同的。
如果您也想将 ProductPricing 映射为 POJO,我认为您不能将其映射为一对一,因为从技术上讲,它是带有过滤器的一对多,希望只产生一个结果。因此,您将拥有 Set。在您的 Product 类中:
使用复合元素,您不需要 PRODUCTPRICING 表中的主键 PPId。
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.