首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么hibernate OneToMany @BatchSize不能工作?

为什么hibernate OneToMany @BatchSize不能工作?
EN

Stack Overflow用户
提问于 2016-10-02 15:37:32
回答 1查看 968关注 0票数 0

我正在使用Spring4.1.4.RELEASE+ hibernate 4.3.6.Final,我正在尝试@BatchSize用于OneToMany,但它似乎不起作用,下面是代码:

代码语言:javascript
复制
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之上,但仍然无法工作。

我想知道我是不是错过了一些配置什么的,有人能帮忙吗?

更新:

这是日志:

代码语言:javascript
复制
[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
EN

回答 1

Stack Overflow用户

发布于 2016-10-03 12:57:08

(摘自评论)

默认情况下,JPA无法告诉getPictures()返回数量有限的图片(afaik)。通常,我认为您不能限制返回的联接对象的数量。

如果要限制find方法返回的图片的数量,就必须编写自己的方法(@BatchSize只限制所做的SELECTS语句的数量,而不是结果的数量)。

您可以使用JPA:在图片上创建一个JPQL查询(而不是产品),然后在.setMaxResult(2)之前添加.getResults() (您可以使用youPicturesList().get(0).getProduct();获得产品)

也许你可以用CriteriaBuilder来做你想做的事情,这可能允许你限制加入的数量,但是我从来没有这样使用过它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39818633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档