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

问题描述 投票:1回答:3

我有一个插入查询,其中与其他字段一起,我插入时间戳。现在,只要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);

这虽然是春天数据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();


    }

虽然查询看起来很长,但您只能关注时间戳字段,这就是产生错误的原因。

这有什么用?

java sql oracle spring-data jpql
3个回答
1
投票

这对我有用

 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


0
投票

它发生的原因是你的表不允许last_modified_date为null值。

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

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

或者只是像这样更改数据库:

ALTER TABLE table_name MODIFY COLUMN date TIMESTAMP NULL

0
投票

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

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

现在要更好地插入日期值以使用Calendar类或Date类。

© www.soinside.com 2019 - 2024. All rights reserved.