Search code examples
javaspringjpabooleanentity

Spring jpa changes entity boolean variable name when return entity object


I have the following Comment entity:

@Entity
@Table(name = "comment")
public class Comment extends AbstractEntity 

And one of the column:

@Column(name = "is_deleted")
    private Boolean isDeleted;

The returned comment object changes the variable name from isDeleted to deleted.

And when I save the Comment object from client call. If I say isDeleted:false, And what I get is deleted:null. And if I say deleted:false, what I get is deleted:false. So looks like the column name is deleted but not isDeleted.

Do not know why this happen.

The whole comment entity code:

package no.nsd.archivingportal.domain.comment;

import no.nsd.archivingportal.domain.AbstractEntity;
import no.nsd.archivingportal.domain.user.User;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.UUID;

@Entity
@Table(name = "comment")
public class Comment extends AbstractEntity {

    @Type(type="pg-uuid")
    @Column(name = "commented_entity")
    private UUID commentedEntity;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User author;

    @Column(name = "content")
    private String content;

    @Column(name = "is_deleted")
    private Boolean isDeleted;

    public UUID getCommentedEntity() {
        return commentedEntity;
    }

    public void setCommentedEntity(UUID commentedEntity) {
        this.commentedEntity = commentedEntity;
    }

    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Boolean getDeleted() {
        return isDeleted;
    }

    public void setDeleted(Boolean deleted) {
        isDeleted = deleted;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        Comment comment = (Comment) o;

        if (commentedEntity != null ? !commentedEntity.equals(comment.commentedEntity) : comment.commentedEntity != null)
            return false;
        if (author != null ? !author.equals(comment.author) : comment.author != null) return false;
        if (content != null ? !content.equals(comment.content) : comment.content != null) return false;
        return isDeleted != null ? isDeleted.equals(comment.isDeleted) : comment.isDeleted == null;

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (commentedEntity != null ? commentedEntity.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (isDeleted != null ? isDeleted.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "commentedEntity=" + commentedEntity +
                ", author=" + author +
                ", content='" + content + '\'' +
                ", isDeleted=" + isDeleted +
                '}';
    }
}

Solution

  • I think the problem is with your getter/setters of isDeleted property.

    So either change the property name to deleted and keep your getter/setters as is or change your getter/setters to reflect more precisely the name of the property e.g.

    public Boolean getIsDeleted() {
        return isDeleted;
    }
    
    public void setIsDeleted(Boolean deleted) {
        isDeleted = deleted;
    }