Hibernate:为什么FetchType.LAZY-annotated集合属性急切加载?

时间:2016-11-05 11:09:48

标签: java hibernate jpa

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

@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")处加上了断点。我检查了物品对象。看到图片: enter image description here

2 个答案:

答案 0 :(得分:13)

通过检查调试器中的对象,您要求它调用列表的方法来显示其大小,内容等。当然,这懒惰地初始化列表。

同样适用于您的class anotherClass{ <fields, constructors, getters/setters, methods> } public class testClass{ public static void main(String[] args) { <creating/using objects> } } 方法,该方法隐含地遍历列表以进行打印。

答案 1 :(得分:0)

作为Hibernate的文档 @ OneToMany - 默认fetchType是LAZY @ ManyToOne - 默认的fetchType是EAGER。

如果你想改变,那么fetch = fetchType.LAZY / EAGER

基本上,hibernate对象有两次1)实体对象2)Valued Object.So在你的情况下Item与Bid有多对多关系,如果你在那时检索Item类,hibernate将无法获取相关的Bid Class记录,因为你确实获取类型是LAZY,但你急切地为你获取Bid Class记录hibernate fetch相关项目,因为Bid与Item类有一个ManyToOne关系,而ManyToOne的默认提取类型是EAGER。