我有以下实体的问题:森林,树,叶.你可以想象森林可以有很多树木,树上有很多树叶.
我想懒得加载森林的所有树木,并渴望加载一棵树的所有树叶.我的hibernate注释代码如下所示:
Forest.java
@Entity
@Table(name = "Forests")
public class Forest implements Comparable<Forest> {
@Id
@Column(name = "forestnumber", length=10, nullable=false)
private String number;
@OneToMany(fetch=FetchType.LAZY, mappedBy="forest")
private Set<Tree> trees = null;
// some other attributes and methods
Run Code Online (Sandbox Code Playgroud)
Tree.java
@Entity
@Table(name = "Trees")
public class Tree implements Comparable<Tree> {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="tree_id", nullable=false)
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "forestnumber", nullable = false)
@Fetch(FetchMode.JOIN)
private Forest forest;
@OneToMany(fetch=FetchType.EAGER, mappedBy="tree")
@Fetch(FetchMode.JOIN)
private Set<Leaf> leafs = null;
// some other attributes and methods …Run Code Online (Sandbox Code Playgroud)