Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 属性在Spring/Hibernate的响应中为空_Java_Javascript_Mysql_Spring_Hibernate - Fatal编程技术网

Java 属性在Spring/Hibernate的响应中为空

Java 属性在Spring/Hibernate的响应中为空,java,javascript,mysql,spring,hibernate,Java,Javascript,Mysql,Spring,Hibernate,我有一个Javascript应用程序,它使用Java作为Hibernate、Spring和MySQL数据库的后端。当读取数据时,一切正常,如预期,但当试图在客户端编辑它时,我可以看到奇怪的行为。即使我的服务器请求如下所示: {“data”:{“Draggable”:true,“Resizable”:true,“StartDate”:“2012-09-13T18:00:00+02:00”,“EndDate”:“2012-09-14T04:00:00+02:00”,“Cls”:“Name”:“Sec

我有一个Javascript应用程序,它使用Java作为Hibernate、Spring和MySQL数据库的后端。当读取数据时,一切正常,如预期,但当试图在客户端编辑它时,我可以看到奇怪的行为。即使我的服务器请求如下所示:

{“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}

服务器响应为:

{“数据”:[{“名称”:“秘密任务”,“Id”:10,“开始日期”:“2012-09-13T18:00:00+02:00”,“结束日期”:“2012-09-14T04:00:00+02:00”,“资源Id”:15,“可调整大小”:null,“可拖动”:null,“Cls”:“}]

其中布尔属性为空。我试着忽略这个领域的二传手,但没有任何运气。我还必须删除
nullable=false
,因为其中包含了一个错误:

org.springframework.dao.DataIntegrityViolationException:notnull属性引用null或瞬时值:model.Event.draggable;嵌套异常为org.hibernate.PropertyValueException:notnull属性引用空值或瞬时值: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;
    }
}

我能做些什么来防止这种行为吗?

你可以简单地做到:

public boolean getResizable() {
    return resizable != null && resizable;
}

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

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

@JsonIgnore
public void setResizable(Boolean resizable) {
    this.resizable = resizable;
}
基本上就是说,从JSON数据反序列化时忽略这些属性。因此,当您保存对象时,这些属性在数据库中为空