外键注释上相同实体的Hibernate OneToOne关系

时间:2015-11-22 12:52:12

标签: java entity-framework hibernate

我有一个名为Post的实体类。该类有一个名为ParentId的字段。这是同一实体类的Foreign key

我用Annotations尝试了几种不同的方法,但我总是遇到一些错误。例如。

org.hibernate.MappingException: Could not determine type for: dk.nwessel.model.Post, at table: Posts, for columns: [org.hibernate.mapping.Column(post)]

我的代码:

@Entity
@Table(name = "Posts", catalog = "StackOverflow")

public class Post {
    private Integer Id, PostTypeId, ParentId, AcceptedAnswerId, Score, ViewCount, OwnerUserId, LastEditorUserId, AnswerCount,
            CommentCount, FavoriteCount;
    private String Body, Title, Tags;
    private Date CreationDate, LastEditDate, LastActivityDate;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "ParentId")
    private Post post;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", unique = true, nullable = false)
    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    @Column(name = "PostTypeId")
    public Integer getPostTypeId() {
        return PostTypeId;
    }

    public void setPostTypeId(Integer postTypeId) {
        PostTypeId = postTypeId;
    }


    public Post getPost(){
        return post;
    }

    @Column(name = "ParentId")
    public Integer getParentId() {
        return ParentId;
    }

    public void setParentId(Integer parentId) {
        ParentId = parentId;
    }

    @Column(name = "AcceptedAnswerId")
    public Integer getAcceptedAnswerId() {
        return AcceptedAnswerId;
    }

    public void setAcceptedAnswerId(Integer acceptedAnswerId) {
        AcceptedAnswerId = acceptedAnswerId;
    }

    @Column(name = "Score")
    public Integer getScore() {
        return Score;
    }

    public void setScore(Integer score) {
        Score = score;
    }
    @Column(name = "ViewCount")
    public Integer getViewCount() {
        return ViewCount;
    }

    public void setViewCount(Integer viewCount) {
        ViewCount = viewCount;
    }
    @Column(name = "OwnerUserId")
    public Integer getOwnerUserId() {
        return OwnerUserId;
    }

    public void setOwnerUserId(Integer ownerUserId) {
        OwnerUserId = ownerUserId;
    }
    @Column(name = "LastEditorUserId")
    public Integer getLastEditorUserId() {
        return LastEditorUserId;
    }

    public void setLastEditorUserId(Integer lastEditorUserId) {
        LastEditorUserId = lastEditorUserId;
    }
    @Column(name = "AnswerCount")
    public Integer getAnswerCount() {
        return AnswerCount;
    }

    public void setAnswerCount(Integer answerCount) {
        AnswerCount = answerCount;
    }
    @Column(name = "CommentCount")
    public Integer getCommentCount() {
        return CommentCount;
    }

    public void setCommentCount(Integer commentCount) {
        CommentCount = commentCount;
    }
    @Column(name = "FavoriteCount")
    public Integer getFavoriteCount() {
        return FavoriteCount;
    }

    public void setFavoriteCount(Integer favoriteCount) {
        FavoriteCount = favoriteCount;
    }
    @Column(name = "Body")
    public String getBody() {
        return Body;
    }

    public void setBody(String body) {
        Body = body;
    }
    @Column(name = "Title")
    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }
    @Column(name = "Tags")
    public String getTags() {
        return Tags;
    }

    public void setTags(String tags) {
        Tags = tags;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "CreationDate")
    public Date getCreationDate() {
        return CreationDate;
    }

    public void setCreationDate(Date creationDate) {
        CreationDate = creationDate;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "LastEditDate")
    public Date getLastEditDate() {
        return LastEditDate;
    }

    public void setLastEditDate(Date lastEditDate) {
        LastEditDate = lastEditDate;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "LastActivityDate")
    public Date getLastActivityDate() {
        return LastActivityDate;
    }

    public void setLastActivityDate(Date lastActivityDate) {
        LastActivityDate = lastActivityDate;
    }

}

1 个答案:

答案 0 :(得分:2)

您还需要引用ParentId指向的位置:

@JoinColumn(name = "ParentId", referencedColumnName = "Id")

编辑:我也看到ParentId不是唯一的。这将使您的关系@ManyToOne而不是@OneToOne