如何在OneToOne关系中从父级延迟获取子实体?

时间:2017-12-11 06:38:25

标签: spring hibernate jpa

我正试图以一对一的关系从父实体中获取延迟子实体。

我尝试在父方添加@LazyToOne(LazyToOneOption.NO_PROXY)。我也尝试了@MapsId,如post& post2。但是,儿童实体仍然热切期待。下面是我的实体课程,我应该做些什么来实现懒惰。

@Entity
@Table(name = "POST")
public class Post {
    @Id
    @GeneratedValue
    private Long id;

    private String title;

    @OneToOne(mappedBy = "post", cascade = CascadeType.ALL,
            fetch = FetchType.LAZY, optional = false)
    private PostDetails details;

    //getters and setters
}


@Entity
@Table(name = "POST_DETAILS")
public class PostDetails {

    @Id
    private Long id;

    private String comment;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private Post post;

    //getters and setters
}


(Post)session.get(Post.class, 1L);

0 个答案:

没有答案