everyone。 我有一個LOG表與觸發器和序列的組合來創建ID,所以當我插入行時,我不必指定ID,否則數據庫會返回錯誤。然而,Hibernate聲稱(正確)是指定的主鍵。帶DB觸發器生成ID的Hibernate映射+序列號
在這種情況下應該使用什麼樣的「發電機」屬性?
我已經嘗試過「分配」它說:
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): it.m2sc.simulator.beans.Log
隨着「選擇」 HIB問我自然鍵,但在這個表中沒有自然鍵,但主。
org.hibernate.id.IdentifierGenerationException: no natural-id property defined; need to specify [key] in generator parameters
這是我的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>
的Class
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表/觸發器/序列
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
Ty in advice
與嘗試「** **本土」過,它說:「從雙重選擇hibernate_sequence.nextval」 :( –