java.sql.SQLException: ORA-00932: 数据类型不一致: 预期的 DATE 在插入空时间戳时得到 BINARY

java.sql.SQLException: ORA-00932: inconsistent datatypes: expected DATE got BINARY while Inserting Null timestamp

我有一个插入查询,我正在插入时间戳以及其他字段。现在,每当 Timestamp 的值为 null 时,我都会收到错误 -

java.sql.SQLException: ORA-00932: inconsistent datatypes: expected DATE got BINARY

我正在使用 Oracle 11g。

查询是:

@Modifying
@Query(value ="INSERT INTO mams_asset a ( a.mams_asset_id, a.mams_folder_id, a.asset_name, a.gist, a.last_modified_date, a.last_exported_date, a.created_date ) VALUES (hextoraw(?1), hextoraw(?2), ?3, ?4, ?5, ?6 , ?7)" , nativeQuery = true)
int insertIntoMamsAsset(String mamsAssetId, String mamsFolderId, String assetName, String gist, Timestamp lastModifiedDate, Timestamp lastExportedDate, Timestamp createdDate);

这虽然是 spring 数据 JPA 之一。我也尝试过使用这种方法但同样的错误:

public int insertIntoMamsAsset(String mamsAssetId, String mamsFolderId, String assetName, String gist, Timestamp lastModifiedDate, Timestamp lastExportedDate, Timestamp createdDate){

        final Query query = entityManager.createNativeQuery("INSERT INTO mams_asset a ( a.mams_asset_id, a.mams_folder_id, a.asset_name, a.gist, a.last_modified_date, a.last_exported_date, a.created_date ) VALUES (hextoraw(?), hextoraw(?), ?, ?, ?, ? , ?)")
                .setParameter(1, mamsAssetId)
                .setParameter(2,mamsFolderId)
                .setParameter(3,assetName)
                .setParameter(4,gist)
                .setParameter(5,lastModifiedDate)
                .setParameter(6,lastExportedDate)
                .setParameter(7,createdDate);

        return query.executeUpdate();


    }

虽然查询看起来很长,但您可以只关注产生错误的时间戳字段。

解决这个问题的方法是什么?

发生这种情况是因为您的 table 不承认 last_modified_date 的空值。

在像这样启动查询之前尝试检查输入值 lastModifiedDate 是否为 null 或 no

if(lastModifiedDate = null){
Timestamp myModifiedDate = new Timestamp(0001-01-01 00:00:00);
}

或者像这样简单地更改数据库:

ALTER TABLE table_name MODIFY COLUMN date TIMESTAMP NULL

当您尝试将值插入不同类型的列以获取更多详细信息时,会出现此 oracle 消息:

http://www.dba-oracle.com/sf_ora_00932_inconsistent_datatypes_expected_string_got_string.htm

现在插入日期值更好地使用日历 class 或日期 class。

这对我有用

 final Query query = entityManager.createNativeQuery("INSERT INTO mams_asset a ( a.mams_asset_id, a.mams_folder_id, a.asset_name, a.gist, a.last_modified_date, " +
                "a.last_exported_date, a.created_date ) VALUES (hextoraw(?), hextoraw(?), ?, ?, ?, ? , ?)")
                .setParameter(1, mamsAssetId)
                .setParameter(2, mamsFolderId)
                .setParameter(3, assetName)
                .setParameter(4, gist)
                .setParameter(5, lastModifiedDate, TemporalType.TIMESTAMP)
                .setParameter(6, lastExportedDate, TemporalType.TIMESTAMP)
                .setParameter(7, createdDate, TemporalType.TIMESTAMP);

我在setParameter中添加了TemporalType.TIMESTAMP