通过数据库触发器+序列生成ID的Hibernate映射 [英] Hibernate mapping with ID generated by DB trigger + sequence

查看:147
本文介绍了通过数据库触发器+序列生成ID的Hibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人。
我有一个LOG表,其中包含触发器和序列的组合以创建ID,因此,当我插入行时,不必指定ID,否则数据库将返回错误。但是,Hibernate声称(正确地)指定了主键。

everyone. I've a LOG table with a combination of trigger and sequence to create the id, so when I insert the line I do not have to specify the id, otherwise the database returns error. However Hibernate claims (rightly) that was specified primary key.

在这种情况下,我应该使用哪种生成器属性?

我已经尝试过 已分配,它说:

I already tried "assigned" it says:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save (): it.m2sc.simulator.beans.Log

使用 选择 hib询问我自然键,但是此表中没有自然键,只有主键。

With "select" hib ask me the natural key, but there is no natural key in this table, but the primary.

org.hibernate.id.IdentifierGenerationException: no natural-id property defined; need to specify [key] in generator parameters

中指定[key]

那是我的hbm

That's my hbm

<hibernate-mapping>
    <class name="it.m2sc.simulator.beans.Log" table="LOG">
        <id name="id" type="integer" column="LOG_ID" access="field">
            <generator class="select" /> 
        </id>
        <property name="date" type="date" column="LOG_DATE" access="field" />
        <property name="user" type="string" column="LOG_USER" access="field" />
        <property name="evtId" type="integer" column="EVT_ID" access="field" />
        <property name="detail" type="string" column="LOG_DETAIL" access="field" />
        <property name="deleted" type="character" column="LOG_DELETED" access="field" />
        <property name="codiceRaggruppamento" column="LOG_CODICE_RAGGRUPPAMENTO" type="string" access="field" />
    </class>
</hibernate-mapping>

班级

public class Log {
    private Integer id;
    private Date date;
    private String user;
    private Integer evtId;
    private String detail;
    private Character deleted = '0';
    private String codiceRaggruppamento;

    ... ( getter & setter )
}

表/触发器/序列的DDL

DDL of table/trigger/sequence

CREATE TABLE
    LOG
    (
        LOG_ID NUMBER(12) NOT NULL,
        LOG_DATE TIMESTAMP(6),
        LOG_USER VARCHAR2(50),
        EVT_ID NUMBER(12),
        LOG_DETAIL VARCHAR2(100),
        LOG_DELETED CHAR(1) DEFAULT '0 ' NOT NULL,
        LOG_CODICE_RAGGRUPPAMENTO NCHAR(2) NOT NULL,
        CONSTRAINT LOG_PK PRIMARY KEY (LOG_ID),
        CONSTRAINT LOG_CFG_EVENT_TYPE_FK1 FOREIGN KEY (EVT_ID)
        REFERENCES CFG_EVENT_TYPE (EVT_ID)
    );

   CREATE SEQUENCE  LOG_SEQ  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 358 CACHE 20 NOORDER  NOCYCLE;

CREATE OR REPLACE TRIGGER "DTCUSR"."LOG_TRG" BEFORE INSERT ON LOG
FOR EACH ROW
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    IF :NEW.LOG_ID IS NULL THEN
      SELECT LOG_SEQ.NEXTVAL INTO :NEW.LOG_ID FROM DUAL;
    END IF;
  END COLUMN_SEQUENCES;
END;

啊,仅供参考:db是Oracle11

Ah, just fyi: db is Oracle11

忠告

推荐答案

要实现该目标,您需要使用GenerationType.IDENTITY

To achieve the goal you need to use GenerationType.IDENTITY

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOG_ID", updatable = false, nullable = false)
private Integer id;

> https://www.thoughts-on-java.org/jpa-generate-primary-keys/

这篇关于通过数据库触发器+序列生成ID的Hibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆