【问题标题】:Error: The object of class String, from mapping could not be converted to [class java.sql.Timestamp]错误:String 类的对象,无法从映射转换为 [class java.sql.Timestamp]
【发布时间】:2017-06-12 13:35:24
【问题描述】:

我启动了一条 sql 语句,试图获取一个带有日期字段的对象并引发错误。我可以毫无问题地保存在我的数据库中,但我无法恢复数据并创建对象。

错误:

Exception [TOPLINK-3002] (Oracle TopLink Essentials - 2.0 (Build b41-beta2 (03/30/2007))): oracle.toplink.essentials.exceptions.ConversionException
Exception Description: The object [https://stackoverflow.com/questions/43551744/php-function-error-object-of-class-pdostatement09/06/17 15:05:08,022000000], of class [class java.lang.String], from mapping [oracle.toplink.essentials.mappings.DirectToFieldMapping[fecha_cambio-->SGSI_DOCUMENTOS.FECHA_CAMBIO]] with descriptor [RelationalDescriptor(com.dominion.procop.sgsi.entidades.DocumentoOnline --> [DatabaseTable(SGSI_DOCUMENTOS)])], could not be converted to [class java.sql.Timestamp].
    at oracle.toplink.essentials.exceptions.ConversionException.incorrectTimestampFormat(ConversionException.java:99)

引发错误的查询:

@NamedQuery(name="ProyContDocumentos.getDocumentosPorPC", query="SELECT p.documento FROM ProyContDocumentos p " +
        "WHERE p.idProyecto = :idProyecto AND p.idControl = :idControl ORDER BY p.idDocumento ASC"),

在线文档:

@Entity
@Table(name = "SGSI_DOCUMENTOS")
@NamedQueries({
    @NamedQuery(name="DocumentoOnline.getAll", query="SELECT d FROM DocumentoOnline d ORDER BY d.id ASC")
})

public class DocumentoOnline extends EntidadBase {

    private static final long serialVersionUID = 6891627122405312774L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id_documento")
    private int                     id;

    @Column(name = "titulo", nullable = false, length = 3000)
    private String                  titulo;

    @Column(name = "descripcion", nullable = false, length = 3000)
    private String                  descripcion;

    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date                fecha_cambio;

    @Column(name = "contenido", nullable = false, length = 4000)
    private String                  contenido;

    /*
     * GETTERS Y SETTERS
     */

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public Date getFecha_cambio() {
        return fecha_cambio;
    }

    public void setFecha_cambio(Date fecha_cambio) {
        this.fecha_cambio = fecha_cambio;
    }

    public String getContenido() {
        return contenido;
    }

    public void setContenido(String contenido) {
        this.contenido = contenido;
    }

    @Override
    public String toString() {
        return "DocumentoOnline [id=" + id + ", titulo=" + titulo + ", descripcion=" + descripcion + ", fecha_cambio="
                + fecha_cambio + ", contenido=" + contenido + "]";
    }
}

数据库:

Column Name.........Data type...length..Allow Nulls
ID_DOCUMENTO........NUMBER..............false
TITULO..............VARCHAR2....4000....false
DESCRIPCION.........VARCHAR2....4000....false
FECHA_CAMBIO........VARCHAR2....1000....false
CONTENIDO...........VARCHAR2....4000....true

如何将该字符串解析为 Date() 并创建我的对象 DocumentoOnline? 问候。

【问题讨论】:

  • 错误信息对我来说似乎很清楚,你的字符串不仅仅是时间戳:https://stackoverflow.com/questions/43551744/php-function-error-object-of-class-pdostatement09/06/17 15:05:08,022000000。我不知道为什么它包含this question: Php function error (Object of class PDOStatement)的URL,我想它来自你的数据库?
  • 也许您应该让我们知道您的数据库表 (SGSI_DOCUMENTOS) 是什么样的?列数据类型以及直接查询 Oracle 数据库时行的外观。
  • @OleV.V.,已更新为 SGSI_DOCUMENTOS 的行。现在我看到 fecha_cambio 的类型是 varchar2 而不是时间戳...
  • 为什么要将日期/时间戳作为字符串存储在表中?根据字符串中的逗号,您似乎遇到了隐式转换问题和NLS_NUMERIC CHARACTERS 设置的差异。您也许可以覆盖它(查看您的语言环境设置),但如果您使用正确的数据类型,生活会更轻松。
  • @AlexPoole ,我不知道为什么它是 varchar。我将列类型修改为 timestamp(6),现在可以完美运行了!

标签: java sql oracle date toplink


【解决方案1】:

表 SGSI_DOCUMENTOS 是:

Column Name.........Data type...length..Allow Nulls
ID_DOCUMENTO........NUMBER..............false
TITULO..............VARCHAR2....4000....false
DESCRIPCION.........VARCHAR2....4000....false
FECHA_CAMBIO........VARCHAR2....1000....false
CONTENIDO...........VARCHAR2....4000....true

我用语句修改了数据类型为FECHA_CAMBIO行:

alter table SGSI_DOCUMENTOS
modify 
( 
   fecha_cambio    timestamp(6)
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多