Hibernate Spring Data本机查询无法使用MERGE INTO更新NULL

时间:2018-07-26 07:06:21

标签: java spring hibernate spring-data nativequery

首先,如果我直接在数据库上运行MERGE INTO,则我有一个有效的查询。我想SET NULLMERGE INTO WHEN MATCHED THEN部分的某些列。当我将它与Hibernate一起使用时,它根本不起作用。

这是我的方法的样子:

@Modifying
@Transactional
@Query(nativeQuery = true,
        value = "MERGE INTO APP_TOKEN t " +
                "USING DUAL " +
                "ON (  t.TYPE      = :appTokenType " +
                "  AND t.TOKEN     = :token " +
                "  AND t.IS_DELETE = 0 " +
                ") " +
                "WHEN MATCHED THEN " +
                "  UPDATE SET " +
                "    t.APP_USER_ID   = :appUserId, " +
                "    t.APP_DEVICE_ID = :appDeviceId, " +
                "    t.EXPIRY_DATE   = :expiryDate, " +
                "    t.UPDATED_DT    = :updatedDt, " +
                "    t.UPDATED_BY    = :updatedBy " +
                "WHEN NOT MATCHED THEN " +
                "  INSERT (APP_USER_ID, APP_DEVICE_ID, TYPE, TOKEN, EXPIRY_DATE, IS_DELETE, STATUS, CREATED_BY, CREATED_DT, UPDATED_BY, UPDATED_DT) " +
                "  VALUES (:appUserId, :appDeviceId, :appTokenType, :token, :expiryDate, :isDelete, :status, :createdBy, :createdDt, :updatedBy, :updatedDt) ")
int upsert(@Param(value = "appUserId")        Long         appUserId,
           @Param(value = "appDeviceId")      Long         appDeviceId,
           @Param(value = "appTokenType")     String       appTokenType,
           @Param(value = "token")            String       token,
           @Param(value = "expiryDate")       Date         expiryDate,
           @Param(value = "isDelete")         Boolean      isDelete,
           @Param(value = "status")           String       status,
           @Param(value = "createdBy")        Long         createdBy,
           @Param(value = "createdDt")        Date         createdDt,
           @Param(value = "updatedBy")        Long         updatedBy,
           @Param(value = "updatedDt")        Date         updatedDt);

该方法的appUserId(第一个参数)可能是NULL,如果参数是NULL,我想将此列更新为NULL(在数据库中) (在Java中)。

appUserIdNULL时,抛出以下异常:

java.sql.sqlsyntaxerrorexception ora-00932 inconsistent datatypes expected number got binary

我使用的框架/库:

  • Spring MVC 5.0.4.RELEASE
  • Spring Data JPA 2.0.0.RELEASE
  • 休眠5.2.16.Final

数据库:Oracle Database 12c标准版12.1.0.2.0-64位生产

Hibernate是否可以使用NULL处理设置UPDATE

编辑: 我尝试使用简单的UPDATE ... SET ...语句将某些列设置为NULL,但仍然没有成功

0 个答案:

没有答案