本文介绍了Hibernate:为什么急于加载FetchType.LAZY注释的collection属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现简单的一对多关联.在使用调试模式检查项目对象后,我发现List< Bid>出价已加载.但是List< Bid> bids属性用FetchType.LAZY注释.一些书籍和网页声称FetchType.LAZY是JPA提供程序接受或拒绝的提示.但是我想知道JPA提供程序在什么条件下忽略FetchType.LAZY.预先谢谢你.

I tried to implement simple one-to-many association. after inspecting the item object with debugging mode, I found that List<Bid> bids already loaded. But List<Bid> bids property is annotated with FetchType.LAZY. some books and web pages claim that FetchType.LAZY is a hint JPA providers accept or reject. But I wonder on what condition JPA providers ignore FetchType.LAZY. Thank you in advance.


@Entity
@Table(name = "ITEM")
public class Item implements Serializable {

    @Id
    private Long id = null;

    private String name;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "SELLER_ID", nullable = false)
    private User seller;

    @OneToMany(mappedBy = "item", fetch = FetchType.LAZY)
    private List<Bid> bids;

    /**
     * No-arg constructor for JavaBean tools.
     */
    public Item() {}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getSeller() {
        return seller;
    }

    public void setSeller(User seller) {
        this.seller = seller;
    }

    @Override
    public String toString() {
        return "Item{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", seller=" + seller +
                ", bids=" + bids +
                '}';
    }
}

@Entity
@Table(name = "BID")
public class Bid implements Serializable {

    @Id @GeneratedValue
    @Column(name = "BID_ID")
    private Long id = null;

    @ManyToOne
    @JoinColumn(name = "ITEM_ID", nullable = false, updatable = false, insertable = false)
    private Item item;

    @ManyToOne
    @JoinColumn(name = "BIDDER_ID", nullable = false, updatable = false)
    private User bidder;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public User getBidder() {
        return bidder;
    }

    public void setBidder(User bidder) {
        this.bidder = bidder;
    }

    @Override
    public String toString() {
        return "Bid{" +
                "id=" + id +
                ", bidder=" + bidder +
                '}';
    }
}

private static void itemSeller(EntityManager em) {

        Item item = em.find(Item.class, 1L);
        System.out.println("hello");

    }

我在语句System.out.println("hello")处放置了断点.我检查了物品对象.看图片:

EDIT : I put break point at the statement System.out.println("hello"). I inspected the item object. see the picture :

推荐答案

通过在调试器中检查对象,您要求它调用列表的方法以显示其大小,内容等.当然,该对象也将初始化懒惰的列表.

By inspecting the object in your debugger, you're asking it to call methods of the list to display its size, content, etc. And that of course initializes the list, lazily.

与您的toString()方法相同,该方法隐式循环遍历列表以进行打印.

Same for your toString() method, which implicitely loops through the list to print it.

这篇关于Hibernate:为什么急于加载FetchType.LAZY注释的collection属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 01:53