休眠 :为什么 FetchType.LAZY 注释的集合属性急切地加载?
我试图实现简单的关联。使用调试模式检查项对象后,我发现已经加载。但是 bids 属性用 注释。一些书籍和网页声称这是JPA提供者接受或拒绝的提示。但是我想知道JPA提供者在什么条件下忽略.提前感谢您。one-to-many
List<Bid> bids
List<Bid>
FetchType.LAZY
FetchType.LAZY
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");
}