gpt4 book ai didi

mysql - @SQLResultSetMapping+Joins 在多个实体上的不同结果 |日本PA

转载 作者:行者123 更新时间:2023-11-29 19:40:08 26 4
gpt4 key购买 nike

我正在使用 JPA 运行 NativeQuery,与在 sql 工具中运行查询相比,它给出了不同的结果。可能我误解了某事。在 @SQLResultSetMapping 的概念内。

--- 概述 ---

我正在将结果映射到两个实体,因此我期望收到实体对列表。这有效。

当您查看下图时,您将看到在sql工具中查询的结果,其中..

  • .. 红框映射到一个实体
  • .. 绿框映射到第二个实体

JPA 应该给我一个 native 行作为一对两个实体。

问题

这就是出问题的地方。是的,我将收到两个实体对的列表,但与图片不同的是,列“pp.id”不会迭代相应表的所有行(在图片“5,6,7,..”中,来自 JPA“5,5,5,5,..”)。

列 pp.id 是一个连接列,我想我在 JPA 中误解了连接 + SQLResultSetMappings 中的某些内容。在我看来,区别在于 JPA 总是从表“propertyprofile”(下面有更多详细信息)连接同一行,这与在 sql 中运行查询时不同。

希望有人可怜我,帮助我。 :)

--- 详细信息 ---

查询

我基本上试图找出是否每个“产品”都为预定义的“属性”(表属性配置文件)定义了“值”(表属性值)。

可能最相关的部分在底部,其中“propertyprofile”被连接,“propertyvalue”被左连接。

select sg.ID as 'sg.id', sg.Name as 'sg.name', ppcount.totalppcount as 'sg.totalppcount', ppcount.totalppothercount as 'sg.totalppothercount',
p.ID as 'product.id', pp.id as 'pp.id', pp.Role as 'pp.role', pp.Name as 'pp.name',
(case when pv.id is null then '0' else '1' end) as 'hasPropertyValue', pv.ID as 'pv.id', pv.StringValue, pv.IntervallMin, pv.IntervallMax
from shoppingguide sg
join
(
select sg.ID as 'sgid', count(*) as 'totalppcount', count(pp_other.ID) as 'totalppothercount' from propertyprofile pp_all
left join propertyprofile pp_other on pp_other.id = pp_all.id AND pp_other.Role = '0'
join shoppingguide sg on pp_all.ShoppingGuideID = sg.ID
join shopifyshop ss on sg.ShopifyShopID = ss.ID
where
pp_all.ShoppingGuideID = sg.ID AND
ss.Name = :shopName
GROUP BY pp_all.ShoppingGuideID
) ppcount on ppcount.sgid = sg.id
join shopifyshop ss on sg.ShopifyShopID=ss.ID
join product p on p.ShopifyShopID = ss.ID
join propertyprofile pp on (pp.ShoppingGuideID = sg.id AND pp.Role = '0')
left join propertyvalue pv on (pv.ProductID=p.ID and pv.PropertyProfileID = pp.id)
where
ss.Name = :shopName
order by sg.id asc, p.id asc, pp.id asc
;

表格

涉及很多表,但这些是理解查询最重要的表:

  • 产品
  • 属性配置文件 - 所有产品都具有的功能(例如高度、价格)
  • propertyvalue - 特定功能的数据;与属性(property)概况相关(例如 5 厘米;120 美元)

SQLResultSetMapping

映射完成到两个实体:ProductDataFillSummary_ShoppingGuideInformation、ProductDataFillSummary_ProductInformation。

@SqlResultSetMapping(
name = "ProductDataFillSummaryMapping",
entities = {
@EntityResult (
entityClass = ProductDataFillSummary_ShoppingGuideInformation.class,
fields = {
@FieldResult(name = "shoppingGuideId", column = "sg.id"),
@FieldResult(name = "shoppingGuideName", column = "sg.name"),
@FieldResult(name = "numberOfTotalPropertyProfiles", column = "sg.totalppcount"),
@FieldResult(name = "numberOfTotalPropertyProfilesOther", column = "sg.totalppothercount")
}),
@EntityResult(
entityClass = ProductDataFillSummary_ProductInformation.class,
fields = {
@FieldResult(name = "productID", column = "product.id"),
@FieldResult(name = "propertyProfileId", column = "pp.id"),
@FieldResult(name = "propertyProfileRole", column = "pp.role"),
@FieldResult(name = "propertyValueId", column = "pv.id"),
@FieldResult(name = "hasPropertyValue", column = "hasPropertyValue")
}
)
})

最佳答案

分析

问题似乎是 Hibernate 不..

  • ..处理每一行
  • .. 每行映射到指定实体
  • ..将此行的映射实体放入列表中(在我的示例中是一对实体)

事实上,hibernate 似乎匹配这两个实体,它们应该根据主键属性进入 List 的同一个条目,即像这样:

  • ..处理每一行
  • .. 每行映射到各自的实体(单独)
  • .. 使用主键存储映射实体
  • .. 匹配进入列表同一条目的各个实体

在我的示例中,一对 [ProductDataFillSummary_ShoppingGuideInformation、ProductDataFillSummary_ProductInformation] 将被插入到列表中。当插入“ProductDataFillSummary_ProductInformation”时,Hibernate 将尝试使用主键(此处为“ProductDataFillSummary_ProductInformation.productId”)查找正确的实例。由于 ProductDataFillSummary_ProductInformation 的多行具有相同的 ProductId 值,因此始终会获取第一个实例并将其用于 List。

解决方案

使用考虑“ProductDataFillSummary_ProductInformation.productId”和“.propertyProfileId”的复合键,或者..

如果无法使用组合 key ,请使用人工 key (uuid):

concat(p.ID, '-', pp.ID) as 'uuid'

关于mysql - @SQLResultSetMapping+Joins 在多个实体上的不同结果 |日本PA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41402161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com