gpt4 book ai didi

java - Spring/Hibernate 响应中的属性为空

转载 作者:行者123 更新时间:2023-11-29 08:40:41 25 4
gpt4 key购买 nike

我有一个 Javascript 应用程序,它使用 Java 作为后端,并带有 Hibernate、Spring 和 MySQL DB。当读取数据时,一切正常且符合预期,但当尝试在客户端编辑它时,我可以看到奇怪的行为。即使我的服务器请求看起来像这样:

{"data":{"可拖动":true,"可调整大小":true,"StartDate":"2012-09-13T18:00:00+02:00","EndDate":"2012 -09-14T04:00:00+02:00","Cls":"","名称":" secret 任务","Id":10,"ResourceId":15}}

服务器响应:

{"data":[{"名称":" secret 任务","Id":10,"StartDate":"2012-09-13T18:00:00+02:00","EndDate ":"2012-09-14T04:00:00+02:00","ResourceId":15,"可调整大小":null,"可拖动":null,"Cls":""}]}

其中 boolean 属性为空。我尝试忽略该字段的 setter ,但没有任何运气。我还必须删除 nullable=false 因为包含此内容时我收到错误:

org.springframework.dao.DataIntegrityViolationException:非空属性引用空值或 transient 值:model.Event.draggable;嵌套异常是 org.hibernate.PropertyValueException:not-null 属性引用 null 或 transient 值:model.Event.draggable

这是我的 MySQL 表定义:

CREATE TABLE IF NOT EXISTS `events` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(255) DEFAULT NULL,
`StartDate` varchar(50) DEFAULT NULL,
`EndDate` varchar(50) DEFAULT NULL,
`ResourceId` int(11) DEFAULT NULL,
`Resizable` tinyint(1) DEFAULT NULL,
`Draggable` tinyint(1) DEFAULT NULL,
`Cls` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

这是模型代码:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonIgnore;

@JsonAutoDetect
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name="events")
public class Event {

@Id
@GeneratedValue
@Column(name="Id")
private int id;

@Column(name="Name", nullable=false)
private String name;

@Column(name="StartDate", nullable=false)
private String startDate;

@Column(name="EndDate", nullable=false)
private String endDate;

@Column(name="ResourceId", nullable=false)
private int resourceId;

@Column(name="Resizable")
private Boolean resizable;

@Column(name="Draggable")
private Boolean draggable;

@Column(name="Cls", nullable=false)
private String cls;

@JsonProperty("Id")
public int getId() {
return id;
}

@JsonProperty("Id")
public void setId(int id) {
this.id = id;
}

@JsonProperty("Name")
public String getName() {
return name;
}

@JsonProperty("Name")
public void setName(String name) {
this.name = name;
}

@JsonProperty("StartDate")
public String getStartDate() {
return startDate;
}

@JsonProperty("StartDate")
public void setStartDate(String start) {
this.startDate = start;
}

@JsonProperty("EndDate")
public String getEndDate() {
return endDate;
}

@JsonProperty("EndDate")
public void setEndDate(String end) {
this.endDate = end;
}

@JsonProperty("ResourceId")
public int getResourceId() {
return resourceId;
}

@JsonProperty("ResourceId")
public void setResourceId(int id) {
this.resourceId = id;
}

@JsonProperty("Resizable")
public Boolean getResizable() {
return resizable;
}

@JsonIgnore
public void setResizable(Boolean resizable) {
this.resizable = resizable;
}

@JsonProperty("Draggable")
public Boolean getDraggable() {
return draggable;
}

@JsonIgnore
public void setDraggable(Boolean draggable) {
this.draggable = draggable;
}

@JsonProperty("Cls")
public String getCls() {
return cls;
}

@JsonProperty("Cls")
public void setCls(String cls) {
this.cls = cls;
}
}

我可以采取什么措施来防止这种行为?

最佳答案

在从 JSON 表示形式反序列化对象时,您明确告诉 Jackson 忽略您提到的两个属性。这:

@JsonIgnore
public void setDraggable(Boolean draggable) {
this.draggable = draggable;
}

@JsonIgnore
public void setResizable(Boolean resizable) {
this.resizable = resizable;
}

基本上意味着,从我的 JSON 数据反序列化时忽略这些属性。因此,当您保存对象时,这些属性在数据库中为空。

关于java - Spring/Hibernate 响应中的属性为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871490/

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