oracle - 如何确定 JPA 中是否发生了 Oracle 可延迟约束违规?

标签 oracle jpa

如果事务违反延迟约束,Oracle 11g 会在提交时静默回滚事务。实际得到错误的一种方法是在调用 commit 之前将约束设置为立即。使用 Oracle SQL Developer 可以正常工作,但在 JPA (EclipseLink) native 查询中执行此操作会导致应用程序锁定。如果违反了延迟约束,是否有其他方法可以通知?或者我没有正确使用 JPA?

表定义:

create table foo
(
foo_id integer not null,
order_id integer not null,
constraint foo_pk primary key (foo_id),
constraint foo_ak unique (order_id) deferrable
);

SQL 生成违规并查看控制台上显示的错误:
set constraint foo_ak deferred;
insert into foo values (1, 1);
insert into foo values (2, 1);
set constraint foo_ak immediate; -- Will rollback and display error

JPA 尝试重新排序无状态 session Bean 中的 foo 实体集合:
public void edit(List<Foo> foos) {
    Query deferred = em.createNativeQuery("set constraint foo_ak deferred");
    Query immediate = em.createNativeQuery("set constraint foo_ak immediate");

    deferred.executeUpdate(); // This works

    for(Foo f: foos) {
        em.merge(f); // This works too
    }

    immediate.executeUpdate(); // Hang with no output!
    // Note: if I comment out above line the transaction may 
    // be silently rolled back on deferred constraint violation
}

最佳答案

问题可能出在语句缓存和应用服务器事务策略中。

尝试:

  • 强制实体管理器通过在执行 {} 后调用 em.flush() 将数据发送到数据库

  • 和/或
  • 在您的方法中将@TransactionManagement 属性更改为bean 管理和管理事务
  • 关于oracle - 如何确定 JPA 中是否发生了 Oracle 可延迟约束违规?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8592622/

    相关文章:

    JPA 错误 : The entity has no primary key attribute defined

    sql - 将列转换为 json 数组的最佳方法 (Oracle)

    sql - 使用字段分隔符连接 sql 查询的结果

    java - 仅映射 JoinTable 关系中的一列

    jpa - 具有空值关系中目标实体的属性的 JPQL LIKE 表达式

    java - JPA find() 不加载实体@Id

    sql - 动态检查变量是否在 PL SQL 中具有值

    sql - 如何判断一个员工是不是经理?

    sql - 如何消除 Oracle-SQL-Table 中含义相同的值?

    spring - @ActiveProfiles 值未分配给配置