2012-12-14 109 views
0

我有一個Javascript應用程序,它使用Java作爲Hibernate,Spring和MySQL DB的後端。在閱讀數據時,一切正常,並且如預期的那樣,但是當試圖在客戶端進行編輯時,我可以看到奇怪的行爲。即使我的服務器請求如下所示:Spring/Hibernate響應的屬性

{"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":""}]}

其中布爾屬性被置空:

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

服務器響應。我試過忽略這個領域的二傳手,但沒有任何運氣。我也不得不刪除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; 
    } 
} 

有什麼我可以做,以防止這種行爲?

回答

1

您明確告訴傑克遜不理你提到的,從它的JSON表示反序列化你的對象時,這兩個屬性。這個:

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

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

基本上是說,從我的JSON數據反序列化時忽略這些屬性。所以,當你保存你的對象時,這些屬性在數據庫中是空的。

1

你可以簡單地這樣做:

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