我正在使用spring 4.1.4.RELEASE + hibernate 4.3.6.Final,我正在为OneToMany尝试@BatchSize,但它似乎不起作用,这里是代码:
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;
}
}
我试图通过id找到Product,但是产品里面没有任何图片,我也尝试将BatchSize放在getPictures之上,但它仍然不起作用。
我想知道我是否错过了某些配置或什么,有人可以帮忙吗?
更新
这是日志:
[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
答案 0 :(得分:0)
(来自评论)
默认情况下,没有办法告诉JPA getPictures()返回有限数量的图片(afaik)。一般来说,我认为你不能限制返回的联接对象的数量。
如果要限制find
方法返回的图片数量,则必须编写自己的方法(@BatchSize
仅限制SELECTS
语句的数量,而不是结果数量)。
您可以使用JPA执行此操作:在图片(而非产品)上创建JPQL查询,然后在.setMaxResult(2)
之前添加.getResults()
(并且您可以使用youPicturesList().get(0).getProduct();
获取产品)
也许你可以用CriteriaBuilder
做你想做的事情,这可能会让你限制加入的参与者,但我从来没有像这样使用它。