为什么休眠OneToMany @BatchSize不起作用? [英] why hibernate OneToMany @BatchSize is not working?
问题描述
我使用spring 4.1.4.RELEASE + hibernate 4.3.6.Final,我正在尝试@BatchSize for OneToMany,但它看起来不起作用,下面是代码:
$ b $创建表产品(
id int(6)unsigned auto_increment主键,
name varchar(30)
); b
创建表格图片(
id int(6)unsigned auto_increment主键,
product_id varchar(30),$ b $ url url varchar(30)
) ;
@Entity(name =product)
public class Product extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
private int id;
@Column(name =name)
私有字符串名称;
@OneToMany(fetch = FetchType.LAZY,mappedBy =product)
@BatchSize(size = 2)
private List< Picture>图片;
公开列表<图片> getPictures(){
返回图片;
}
public void setPictures(List< Picture> pictures){
this.pictures = pictures;
$ b @Entity(name =picture)
@BatchSize(size = 10)
public class图片扩展BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
private int id;
@Column(name =url)
private String url;
$ b @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =product_id,referencedColumnName =id)
private Product product;
public Product getProduct(){
return product;
}
public void setProduct(Product product){
this.product = product;
}
}
@Repository
public class ProductDao extends AbstractHibernateDao< Product>执行IProductDao {
public ProductDao(){
super();
setClazz(Product.class);
}
@Override
public Product find(final int id){
Product product =(Product)getCurrentSession()。get(clazz,id);
System.out.println(* -------------------- find ----------------- --------);
System.out.println(product.getPictures());
System.out.println(* -------------------- end ----------------- --------);
退货产品;
$ / code>
我试图通过id来查找产品,但是该产品里面没有任何图片,我试图把BatchSize放在getPictures上面,但它仍然不起作用。
我想知道是否我错过了一些配置什么的,任何人都可以提供帮助?
更新:
以下是日志:
$ b
[DEBUG] 2016-10-03 17:20:57.074 RequestMappingHandlerMapping:getHandlerInternal [302]:返回处理程序方法[public com.lehoolive.analyse.model .REesponse com.lehoolive.analyse.controller.ProductController.detail(int)]
[DEBUG] 2016-10-03 17:20:57.075 DispatcherServlet:doDispatch [931]:Last-Modified value for [/ product / detail / 1]为:-1
Hibernate:选择product0_.id作为id2_0_,product0_.name作为name2_0_ from product product0_ where product0_.id =?
* -------------------- find ------------------------ -
Hibernate:选择pictures0_.product_id作为product3_2_1_,pictures0_.id作为id1_,pictures0_.id作为id1_0_,pictures0_.product_id作为product3_1_0_,pictures0_.url作为url1_0_从图片pictures0_ where pictures0_.product_id =?
[com.lehoolive.analyse.entity.Picture@29a0ce34,com.lehoolive.analyse.entity.Picture@5a7a10d8,com.lehoolive.analyse.entity.Picture@3e80350]
* ---- ----------------结束-------------------------
[DEBUG] 2016 -10-03 17:20:57.333 ResponseBodyAdviceChain:invoke [61]:为body = com.lehoolive.analyse.model.Response@59141f65调用ResponseBodyAdvice链
(来自评论)
默认情况下无法识别JPA认为getPictures()返回有限数量的图片(afaik)。一般来说,我不认为你可以限制返回的连接对象的数量。
如果您想限制 find
方法返回的图片数量,您必须编写自己的方法( @BatchSize
仅限制 SELECTS
语句的数量,而不是结果数量)。
您可以使用JPA完成此操作:在图片(而非产品)上创建JPQL查询,然后添加 也许你可以用 I am using spring 4.1.4.RELEASE + hibernate 4.3.6.Final, I am trying @BatchSize for OneToMany, but it seems not working, here is the code: I tried to find Product by id, however the product doesn't contain any pictures inside, I tried to put the BatchSize above the getPictures as well, but it still doesn't work. I am wondering if I missed some configuration or something, could anyone help? UPDATE:
Here is the log:
(From the comments) There is no way to tell by default JPA that the getPictures() return a limited number of pictures (afaik). In general, I don't think you can limit the number of joined objects returned. If you want to limit the number of pictures returned by the You can do this with JPA: create a JPQL query on Pictures (not Product), then add Maybe you can do what you want with the 这篇关于为什么休眠OneToMany @BatchSize不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! .setMaxResult(2)$ c ($)之前的$ c> .getResults()
(你可以用 youPicturesList()。get(0).getProduct() code $)
CriteriaBuilder
来做你想做的事,这可能会限制你在加入entites,但我从来没有像这样使用它。create table product (
id int(6) unsigned auto_increment primary key,
name varchar(30)
);
create table picture (
id int(6) unsigned auto_increment primary key,
product_id varchar(30),
url varchar(30)
);
@Entity(name = "product")
public class Product extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
@BatchSize(size=2)
private List<Picture> pictures;
public List<Picture> getPictures() {
return pictures;
}
public void setPictures(List<Picture> pictures) {
this.pictures = pictures;
}
}
@Entity(name = "picture")
@BatchSize(size=10)
public class Picture extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "url")
private String url;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id", referencedColumnName = "id")
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
@Repository
public class ProductDao extends AbstractHibernateDao<Product> implements IProductDao {
public ProductDao() {
super();
setClazz(Product.class);
}
@Override
public Product find(final int id) {
Product product = (Product) getCurrentSession().get(clazz, id);
System.out.println("*--------------------find-------------------------");
System.out.println(product.getPictures());
System.out.println("*--------------------end-------------------------");
return product;
}
}
[DEBUG] 2016-10-03 17:20:57.074 RequestMappingHandlerMapping:getHandlerInternal[302]: Returning handler method [public com.lehoolive.analyse.model.IResponse com.lehoolive.analyse.controller.ProductController.detail(int)]
[DEBUG] 2016-10-03 17:20:57.075 DispatcherServlet:doDispatch[931]: Last-Modified value for [/product/detail/1] is: -1
Hibernate: select product0_.id as id2_0_, product0_.name as name2_0_ from product product0_ where product0_.id=?
*--------------------find-------------------------
Hibernate: select pictures0_.product_id as product3_2_1_, pictures0_.id as id1_, pictures0_.id as id1_0_, pictures0_.product_id as product3_1_0_, pictures0_.url as url1_0_ from picture pictures0_ where pictures0_.product_id=?
[com.lehoolive.analyse.entity.Picture@29a0ce34, com.lehoolive.analyse.entity.Picture@5a7a10d8, com.lehoolive.analyse.entity.Picture@3e80350]
*--------------------end-------------------------
[DEBUG] 2016-10-03 17:20:57.333 ResponseBodyAdviceChain:invoke[61]: Invoking ResponseBodyAdvice chain for body=com.lehoolive.analyse.model.Response@59141f65
find
method, you have to write your own method (@BatchSize
only limits the number of SELECTS
statements made, not the number of result). .setMaxResult(2)
before .getResults()
(and you can get your product with youPicturesList().get(0).getProduct();
)CriteriaBuilder
which may allow you to limit on joined entites, but I've never used it like this.