猿问

一对一的关系不起作用

Hibernate 版本 - 5.3.4.Final,mysql-connector 版本 - 8.0.12


我@OneToOne在帖子和帖子内容之间有一种关系:


邮政:


@Entity

@Table(name = "postsInfo")

public class PostsInfo {



    private long postId;

    private String title;

    private java.util.Date createDate;

    private Integer views;

    private Collection<Tags> tagsPost;

    private PostContent postContent;

    private PostImage postImage;

    private UserInfo userInfo;

    private List<PostsComments> postsComments;


    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "post_id", unique = true, nullable = false)

    public long getPostId() {

        return postId;

    }


    @Basic

    @Column(name = "title", nullable = false)

    public String getTitle() {

        return title;

    }


    @Basic

    @Column(name = "createDate", nullable = false)

    public java.util.Date getCreateDate() {

        return createDate;

    }


    @Basic

    @Column(name = "views")

    public Integer getViews() {

        return views;

    }


    public PostsInfo(){}


    public PostsInfo(String title, List<Tags> tagsPost) {

        this.title = title;

        this.tagsPost = tagsPost;

        this.createDate = new Date();

    }


    @Fetch(FetchMode.SUBSELECT)

    @ManyToMany(fetch = FetchType.EAGER)

    @JoinTable(name = "PostsTags",

            joinColumns = @JoinColumn(name = "post_id"),

            inverseJoinColumns = @JoinColumn(name = "tag_id"))

    public Collection<Tags> getTagsPost() {

        return tagsPost;

    }


    @OneToOne(cascade = CascadeType.ALL, mappedBy = "postsInfo", fetch = FetchType.LAZY, optional = false)

    public PostContent getPostContent() {

        return postContent;

    }


    @OneToOne(cascade = CascadeType.ALL, mappedBy = "postsInfo", orphanRemoval = true, fetch = FetchType.EAGER)

    public PostImage getPostImage() {

        return this.postImage;

    }


    @ManyToOne(fetch = FetchType.LAZY, optional = false)

    @JoinColumn(name = "user_id", nullable = false)

    public UserInfo getUserInfo() {

        return userInfo;

    }


翻阅古今
浏览 132回答 2
2回答

撒科打诨

这意味着您要对该相关集合进行另一个查询。无论如何,延迟加载会生成另一个查询。
随时随地看视频慕课网APP

相关分类

Java
我要回答