JPA实体不使用@Column注释

发布于 2024-10-03 07:22:26 字数 3583 浏览 3 评论 0原文

我有以下简单实体:

   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 中看到,如果检测到的日期是一个空字符串(其中如果 Smooks 转换的源 XML 中缺少该属性),则 getter 应该返回 null。

调试这部分代码确实返回null。

这几乎就像没有使用 getter 来获取 detectorDate 的值。如果是的话,它要么为空,要么至少是一个空格字符串。

I have the following simple entity:

   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;
 }

    }

and this is Smooks the config I'm using:

<?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>

When I try and save the entity, I get the following error:

Data truncation: Incorrect datetime value: '' for column 'detectedDate' at row 1

You can see from the getter for detectedDate that if the detectedDate is an empty string (which it is if that attribute is missing from the source XML for the Smooks transformation) then the getter should return null.

Debugging this part of the code it does indeed return null.

It's almost like the getter isn't being used to get the value for detectedDate. If it were it would either be null, or at least a single space string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-10-10 07:22:26

在回答我自己的问题时,问题来自于这样的事实:您只能注释成员定义或方法,而不能同时注释两者。请注意,我已经注释了 @Id 成员变量 &将其他注释放在方法上。

将 @Id 注解移至该字段的 getter 即可解决问题。

In answer to my own question, the problem comes from the fact you can only annotate the member definitions or the methods, but not both. Notice I had annotated the @Id member variable & put the other annotations on the methods.

Move the @Id annotation to the getter for that field to solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文