gpt4 book ai didi

java - Spring jpa 在返回实体对象时更改实体 boolean 变量名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:00 33 4
gpt4 key购买 nike

我有以下 Comment 实体:

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

还有其中一列:

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

返回的注释对象将变量名从 isDeleted 更改为 deleted。

当我从客户端调用中保存 Comment 对象时。如果我说 isDeleted:false,我得到的是 deleted:null。如果我说 deleted:false,我得到的是 deleted:false。所以看起来列名已删除但不是 isDeleted。

不知道为什么会这样。

整个评论实体代码:

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 +
'}';
}
}

最佳答案

我认为问题出在 isDeleted 属性的 getter/setter 上。

因此,要么将属性名称更改为 deleted 并保持您的 getter/setter 不变,要么更改您的 getter/setter 以更准确地反射(reflect)属性的名称例如

public Boolean getIsDeleted() {
return isDeleted;
}

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

关于java - Spring jpa 在返回实体对象时更改实体 boolean 变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853855/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com