Ondrej Tokar
Ondrej Tokar

Reputation: 5070

Hibernate OneToOne relation of the same entity on Foreign key annotation

I have an entity class called Post. That class has field called ParentId. That is a Foreign key of the same entity class.

I have tried several different approaches with Annotations, but I always experience some error. E.g.

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

My code:

@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;
    }

}

Upvotes: 0

Views: 1935

Answers (1)

Albert Bos
Albert Bos

Reputation: 2062

You also need to reference where ParentId is pointing to:

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

EDIT: Also I see that ParentId is not unique. This will make your relation @ManyToOne and not @OneToOne

Upvotes: 1

Related Questions