java - Hibernate:多重选择@OneToMany

标签 java hibernate

我有三个实体:

Users(id, username, password...); BoughtItems(id, userid, itemcat, itemid); ItemCustomizations(id(id of item), slot(id of color), color)

当我在 BoughtItems.java 中像这样获取时:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "id")
@OrderBy("slot")
private List<ItemCustomization> customColors = new ArrayList<>();

Hibernate 向数据库询问每个项目:

Hibernate: select customcolo0_.itemid as itemid1_2_0_, customcolo0_.itemid 
as itemid1_3_0_, customcolo0_.slot as slot2_3_0_, customcolo0_.itemid as 
itemid1_3_1_, customcolo0_.slot as slot2_3_1_, customcolo0_.color as 
color3_3_1_ from shop_customizations customcolo0_ where 
customcolo0_.itemid=? order by customcolo0_.slot

我的问题

如何正确优化我的映射以减少 mysql 服务器性能影响?

我可以以某种方式将查询/项目优化为 1 个查询吗?

最佳答案

我相信您正在寻找 hibernate 的批量大小功能。请参阅example在这里。

批量大小定义应加载多少个集合。例如,如果 session 中有 10 个 BoughtItems 且批量大小 =5 ,则只会触发两个 sql 查询来获取所有可能的 BoughtItems

customColors 集合

参见Possible fetching strategies hibernate 中

关于java - Hibernate:多重选择@OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377231/

相关文章:

java - 无法在 Java API 中运行 Tensorflow 预测

java - 如何在不使用任何网络服务的情况下从 IP 地址获取国家/地区代码?

java - 扩展应用程序的扩展抛出 InvocationTargetException

java - Spring data Jpa 与 Spring Jdbc 用于查找表

java - Jersey Web 服务和 hibernate 一对多映射

java - 无法加载 JDBC 驱动程序。为什么? ( Spring , hibernate )

java - 存储字符串且忽略字母大小写的数据结构

java - 在根目录下创建文件,奇怪的响应

java - Hibernate 参数值 [568903] 与预期类型不匹配 [java.lang.Long]

带有 Hibernate 和 C3P0 : How do I disable connecting through SSL? 的 Mysql 8.0.11