是否可以在 Spring Data JPA Repository 的 @Query 注释中使用 Oracle Anonymous Block?

尼特卡丁车

我有一个 Emp 表和关联的 JPA 实体 - 员工。Emp 表具有 id、name 和 is_active 列。还有一个资产表,其中包含对 Emp 表的 FK 引用。资产表有 id、emp_id 和 name。

我想软删除(更新 is_active='N')员工,但使用读取的存储库方法删除关联的资产

public interface EmployeeRepository implements JpaRepository<Employee, Long>{
    @Query(
       nativeQuery = true,
       value="BEGIN"+
             "   delete from assets where emp_id = :employeeId;"+
             "   update emp set is_active= 'N' where id = :employeeId;" +
             "END" 
    )
    public void inactivate(@Param("employeeId") Long employeeId);
}

上面的例子是为了说明目的。当我在我的应用程序实体上尝试类似的方法时,我从 Hibernate 类中收到错误。

PS:我知道还有其他方法,如 Hibernates 的级联功能等,但我对 Oracle 匿名块的使用特别感兴趣。

米奇-II

在 spring boot starter 数据 jpa 1.2.5 (hibernate 4.3.10)、oracle 12c 上测试。

不行:

@Modifying
@Query(value = "begin insert into T (ID,A) values (:id, :a); exception when dup_val_on_index then update T set A = :a where ID = :id; end;", nativeQuery = true)
void upsertWatchdog(@Param("id") Long id, @Param("a") Date a);

要么:

@Modifying
@Query(value = "begin insert into T (ID,A) values (?1, ?2); exception when dup_val_on_index then update T set A = ?2 where ID = ?1; end;", nativeQuery = true)
void upsertWatchdog(Long id, Date a);

作品:

@Modifying
@Query(value = "begin insert into T (ID,A) values (?1, ?2); exception when dup_val_on_index then update T set A = ?2 where ID = ?1 ; end;", nativeQuery = true)
void upsertWatchdog(Long id, Date a);

要么:

@Modifying
@Query(value = "begin insert into T (ID,A) values (:id, :a); exception when dup_val_on_index then update T set A = :a where ID = :id ; end;", nativeQuery = true)
void upsertWatchdog(@Param("id") Long id, @Param("a") Date a);

要么:

@Modifying
@Query(value = "begin insert into T (ID,A) values (?, ?); exception when dup_val_on_index then update T set A = ? where ID = ?; end;", nativeQuery = true)
void upsertWatchdog(Long id, Date a, Long id2, Date a2);

关键部分是在命名/定位参数和分号之间使用空格,因为 hibernate 将命名/定位参数名称视为id;而不是id

但我不确定它是否有效,因为它是一个功能还是因为它是一个错误;-)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Data JPA的@PersistenceConstructor注释是否可以与Hibernate结合使用?

Spring Data JPA @Query注释的子句错误

spring data jpa @query是否支持投影?

是否可以将Spring Data JPA中的@Lock与@Modifying @Query结合在一起使用Hibernate?

在IN查询中使用Spel的Spring Data JPA @ Query

Spring Data(JPA)-在@Query中使用泛型

是否可以在Spring Data JDBC(不是JPA)中使用BYTEA字段

Spring Data JPA Repository Inject在Spring Boot中使用@Autowired失败

我可以在Spring Data Repository中使用Mongock吗?

是否可以在Spring Repository中使用原始SQL

使用spring-data-jpa @Query注释查找最大查询结果

如何使用Spring Data JPA @Query注释直接获取学生列表

spring data jpa是否可以与spring-core版本3一起使用

使用@Query 时,我可以更改 Spring Data JPA 中的方法名称吗?

使用Spring Data JPA在Hibernate中使用xml配置而不是@Type注释

为什么我们需要在 Spring Data JPA 中使用 @Transactional 注释 Service 类

Spring Data JPA是否可以使用方法名称解析来汇总实体列?

是否可以使用带有Spring Data JPA的EntityManager创建视图?

Spring Data Jpa Repository可以用于创建复杂的查询吗?

Spring Data JPA:使用@Query中的属性作为参数

Spring Data JPA是否可以防止SQL注入

在 Spring JPA 中使用 Spring Data JPA 的问题

Spring Data JPA调用Oracle Function

Oracle中的Spring Data JPA + EclipseLink

Spring Data Jpa Oracle 无法创建表

Spring Data JPA:在查询方法上使用@Query注释获取第一条记录

如何在Spring Data中使用@query注释指定要返回的字段?

为什么在Spring Data JPA Repository上的save()之后使用返回的实例?

为什么使用JUnit 5运行此Spring Data JPA Repository测试会导致ParameterResolutionException?