一尘不染

Hibernate + Oracle序列+触发器

hibernate

我有一个表,该表的索引自动由使用序列的触发器填充(Oracle数据库)

CREATE TABLE A
(
  IDS                           NUMBER(10)      NOT NULL
)


CREATE OR REPLACE TRIGGER A_TRG
BEFORE INSERT
ON A REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
  :new.IDS := A_SEQ.nextval;
END A_TRG;
/

我有一个匹配的Java类:

Class A {
   @Id
   @SequenceGenerator(name = "aSequence", sequenceName = "A_SEQ", allocationSize = 1)
   @GeneratedValue(generator = "aSequence", strategy = GenerationType.SEQUENCE)
   @Column(name = "IDS")
   Long id;

   ...
}

当我尝试持久保存A的实例时,如下所示:

EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
A a = new A();
Long id = getHibernateTemplate().save(a);
transaction.commit();

我得到这个问题:

  • 保存调用返回的代码中的ID = “ X”

  • 数据库中的ID = “ X + 1”

有没有一种方法可以设置Hibernate来让数据库触发器创建ID?

谢谢


阅读 234

收藏
2020-06-20

共1个答案

一尘不染

HIbernate问题上发现的与Oracle触发器有关的响应,用于从序列中生成ID

我需要调整触发器以仅在未提供ID的情况下运行:

CREATE OR REPLACE TRIGGER A_TRG
BEFORE INSERT
ON A REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
WHEN (New.IDS is null) -- (1)
BEGIN
  :new.IDS := A_SEQ.nextval;
END A_TRG;
/

(1)此行允许Hibernate手动调用A_SEQ.nextVal设置ID,然后绕过触发器,否则Hibernate将无用地获取nextval,因为触发器将始终重置ID,再次调用nextval

2020-06-20