我有一个 Javascript 应用程序,它使用 Java 作为 Hibernate、Spring 和 MySQL DB 的后端。读取数据时,一切正常且符合预期,但尝试在客户端对其进行编辑时,我会看到奇怪的行为。即使我的服务器请求如下所示:
{"data":{"Draggable":true,"Resizable":true,"StartDate":"2012-09-13T18:00:00+02:00","EndDate":"2012-09-14T04:00:00+02:00","Cls":"","Name":"Secret task","Id":10,"ResourceId":15}}
服务器响应:
{"data":[{"Name":"Secret task","Id":10,"StartDate":"2012-09-13T18:00:00+02:00","EndDate":"2012-09-14T04:00:00+02:00","ResourceId":15,"Resizable":null,"Draggable":null,"Cls":""}]}
其中布尔属性为空。我试过忽略这个领域的二传手,但没有任何运气。我还必须删除nullable=false
,因为我收到了一个错误:
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: model.Event.draggable; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: 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;
}
}
我能做些什么来防止这种行为吗?