Spring Roo 生成大量查询
我正在尝试学习 Spring Roo,并且正在做我的小应用程序。 我有产品类:
@RooJavaBean
@RooToString
@RooEntity(table = "TOWARY")
public class Product {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "NAZWA")
private String name;
@Version
@Column(name = "VERSION")
private int version;
}
和事实类:
@RooJavaBean
@RooToString
@RooEntity(table = "FACTS")
public class Fact {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "KWOTA")
private float kwota;
@Column(name = "NCZAS")
private int nczas;
@Version
@Column(name = "VERSION")
private int version;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ID_TOWAR")
private Product product;
}
在 Spring Roo 完成了它的魔力之后,我开始应用程序并看到网页。干得好,代码很少,而且几乎可以工作。
在进行产品预览时,它工作正常:
Hibernate: select product0_.ID as ID0_, product0_.NAZWA as NAZWA0_, product0_.VERSION as VERSION0_ from TOWARY product0_
Hibernate: select product0_.ID as ID0_, product0_.NAZWA as NAZWA0_, product0_.VERSION as VERSION0_ from TOWARY product0_ limit ?
Hibernate: select count(product0_.ID) as col_0_0_ from TOWARY product0_ limit ?
但是当我查看事实时,我有很多查询(基本上每个事实表行一个查询):
Hibernate: select fact0_.ID as ID1_, fact0_.KWOTA as KWOTA1_, fact0_.NCZAS as NCZAS1_, fact0_.ID_TOWAR as ID5_1_, fact0_.VERSION as VERSION1_ from FACTS fact0_
Hibernate: select product0_.ID as ID0_0_, product0_.NAZWA as NAZWA0_0_, product0_.VERSION as VERSION0_0_ from TOWARY product0_ where product0_.ID=?
Hibernate: select product0_.ID as ID0_0_, product0_.NAZWA as NAZWA0_0_, product0_.VERSION as VERSION0_0_ from TOWARY product0_ where product0_.ID=?
Hibernate: select product0_.ID as ID0_0_, product0_.NAZWA as NAZWA0_0_, product0_.VERSION as VERSION0_0_ from TOWARY product0_ where product0_.ID=?
...
搜索后,我发现了“N+1 选择问题”帖子。我说得对吗——这也是我的问题吗?
我认为 @Fetch(FetchMode.JOIN) 强制 hibernate 使用 join 而不是子选择。
在方面文件中,我发现生成的查询负责获取我的数据:
public static List<Fact> Fact.findAllFacts() {
return entityManager().createQuery("SELECT o FROM Fact o", Fact.class).getResultList();
}
How can I force Spring Roo to use join?我做错了什么?
I'm trying to learn Spring Roo, and I'm doing my little application.
I have product class:
@RooJavaBean
@RooToString
@RooEntity(table = "TOWARY")
public class Product {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "NAZWA")
private String name;
@Version
@Column(name = "VERSION")
private int version;
}
And a fact class:
@RooJavaBean
@RooToString
@RooEntity(table = "FACTS")
public class Fact {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "KWOTA")
private float kwota;
@Column(name = "NCZAS")
private int nczas;
@Version
@Column(name = "VERSION")
private int version;
@NotNull
@ManyToOne(fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ID_TOWAR")
private Product product;
}
After Sprinng Roo has done it magic, I'm starting application and I see webpage. Nice work, so little code and it's almost working.
When doing preview of products it's working fine:
Hibernate: select product0_.ID as ID0_, product0_.NAZWA as NAZWA0_, product0_.VERSION as VERSION0_ from TOWARY product0_
Hibernate: select product0_.ID as ID0_, product0_.NAZWA as NAZWA0_, product0_.VERSION as VERSION0_ from TOWARY product0_ limit ?
Hibernate: select count(product0_.ID) as col_0_0_ from TOWARY product0_ limit ?
But when I'm viewing facts I have a lot of queries (basically one for each fact table row):
Hibernate: select fact0_.ID as ID1_, fact0_.KWOTA as KWOTA1_, fact0_.NCZAS as NCZAS1_, fact0_.ID_TOWAR as ID5_1_, fact0_.VERSION as VERSION1_ from FACTS fact0_
Hibernate: select product0_.ID as ID0_0_, product0_.NAZWA as NAZWA0_0_, product0_.VERSION as VERSION0_0_ from TOWARY product0_ where product0_.ID=?
Hibernate: select product0_.ID as ID0_0_, product0_.NAZWA as NAZWA0_0_, product0_.VERSION as VERSION0_0_ from TOWARY product0_ where product0_.ID=?
Hibernate: select product0_.ID as ID0_0_, product0_.NAZWA as NAZWA0_0_, product0_.VERSION as VERSION0_0_ from TOWARY product0_ where product0_.ID=?
...
After searching I have found "N+1 selects problem" posts. Am I right – that also my problem?
I thought that @Fetch(FetchMode.JOIN)
forces hibernate to use join and not subselects.
In aspect files I've found generated query which is responsible for fetching my data:
public static List<Fact> Fact.findAllFacts() {
return entityManager().createQuery("SELECT o FROM Fact o", Fact.class).getResultList();
}
How can I force Spring Roo to use join? What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
FetchType.EAGER
从 ManyToOne 更改为FetchType.LAZY
。预加载正在获取整个对象图。我非常确定您可以摆脱@Fetch(FetchMode.JOIN)
注释。Change
FetchType.EAGER
from the ManyToOne toFetchType.LAZY
. Eager-loading is fetching the whole object graph. And I'm pretty-sure you can get rid of the@Fetch(FetchMode.JOIN)
annotation.