如何使用hibernate将结果集从mysql映射到pojo?

发布于 2024-12-23 03:17:14 字数 679 浏览 3 评论 0原文

我有一个产品表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-12-30 03:17:15

这将给出稍微不同的查询(使用子查询而不是联接),但结果应该是相同的。

@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;
}

如果您也想将 ProductPricing 映射为 POJO,我认为您不能将其映射为一对一,因为从技术上讲,它是带有过滤器的一对多,希望只产生一个结果。因此,您将拥有 Set。在您的 Product 类中:

<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>

使用复合元素,您不需要 PRODUCTPRICING 表中的主键 PPId。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文