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.
What kind of "generator" property should I use in this case?
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
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
That's my hbm
The 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 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
<>
BEGIN
IF :NEW.LOG_ID IS NULL THEN
SELECT LOG_SEQ.NEXTVAL INTO :NEW.LOG_ID FROM DUAL;
END IF;
END COLUMN_SEQUENCES;
END;
Ah, just fyi: db is Oracle11
Ty in advice
解决方案
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;