我有三个实体:
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/