小编Jen*_*her的帖子

懒惰加载孩子,里面有热切的收藏

我有以下实体的问题:森林,树,叶.你可以想象森林可以有很多树木,树上有很多树叶.

我想懒得加载森林的所有树木,并渴望加载一棵树的所有树叶.我的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)

java hibernate lazy-loading

7
推荐指数
1
解决办法
429
查看次数

标签 统计

hibernate ×1

java ×1

lazy-loading ×1