Hibernate:多选@OneToMany

时间:2016-02-13 07:20:11

标签: 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个查询吗?

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找hibernate的批量大小功能。请在此处查看example

批量大小定义应加载的集合数。例如,如果在批处理大小= 5的会话中有10个BoughtItems,则只会触发两个SQL查询以获取所有可能的customColors

BoughtItems个集合

请参阅hibernate中的Possible fetching strategies