我有以下简单的实体:
package net.plus.msodb.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(schema="msodb", name="mso")
public class Mso implements Serializable {
@Id
private Integer incidentReference;
private String detectedDate;
private String detectedTime;
private String startDate;
private String startTime;
private String anticipatedClearDate;
private String anticipatedClearTime;
private String actualClearDate;
private String actualClearTime;
private String headline;
private String progress;
private String details;
private String servicesType;
private String servicesCount;
public Mso() {
}
@Column(name="detectedDate")
public String getDetectedDate() {
if(detectedDate == "") {
return null;
}
return detectedDate + " " + detectedTime;
}
/*
* Getters & Setters removed to save space
*/
@Column(name="detectedDate")
public void setDetectedDate(String detectedDate) {
this.detectedDate = detectedDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public void setAnticipatedClearDate(String anticipatedClearDate) {
this.anticipatedClearDate = anticipatedClearDate;
}
public void setActualClearDate(String actualClearDate) {
this.actualClearDate = actualClearDate;
}
}
这是Smooks我正在使用的配置:
<?xml version="1.0" encoding="UTF-8"?><smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="inputType">input.xml</param>
<param name="input.xml" type="input.type.actived">Workspace://MSODBActions/src/test/resources/msos.xml</param>
</params>
<jb:bean beanId="Mso" class="net.plus.msodb.model.Mso" createOnElement="/msos/mso">
<jb:value data="/msos/mso/@actualClearDate" property="actualClearDate"/>
<jb:value data="/msos/mso/@actualClearTime" property="actualClearTime"/>
<jb:value data="/msos/mso/@anticipatedClearDate" property="anticipatedClearDate"/>
<jb:value data="/msos/mso/@anticipatedClearTime" property="anticipatedClearTime"/>
<jb:value data="/msos/mso/@details" property="details"/>
<jb:value data="/msos/mso/@detectedDate" property="detectedDate"/>
<jb:value data="/msos/mso/@detectedTime" property="detectedTime"/>
<jb:value data="/msos/mso/@headline" property="headline"/>
<jb:value data="/msos/mso/@incidentReference" decoder="Integer" property="incidentReference"/>
<jb:value data="/msos/mso/@progress" property="progress"/>
<jb:value data="/msos/mso/@servicesCount" property="servicesCount"/>
<jb:value data="/msos/mso/@servicesType" property="servicesType"/>
<jb:value data="/msos/mso/@startDate" property="startDate"/>
<jb:value data="/msos/mso/@startTime" property="startTime"/>
</jb:bean>
</smooks-resource-list>
当我尝试保存实体时,出现以下错误:
Data truncation: Incorrect datetime value: '' for column 'detectedDate' at row 1
你可以从getter中看到detectDate,如果detectedDate是一个空字符串(如果Smooks转换的源XML中缺少该属性的话),那么getter应该返回null。
调试这部分代码确实会返回null。
这几乎就像没有使用getter来获取detectedDate的值。如果它是空的,或者至少是一个空格字符串。
答案 0 :(得分:0)
在回答我自己的问题时,问题来自于您只能注释成员定义或方法,而不是两者。注意我注释了@Id成员变量&amp;把其他注释放在方法上。
将@Id注释移动到该字段的getter以解决问题。