Hibernate sequence should only generate when ID is <=0

Posted by Tim Leys on Stack Overflow See other posts from Stack Overflow or by Tim Leys
Published on 2010-04-15T13:00:18Z Indexed on 2010/04/15 13:03 UTC
Read the original article Hit count: 319

Filed under:
|
|
|

Hi all,

I'm using the folowing sequence in my code. (I got the sequence and

@Id
 @SequenceGenerator(name = "S912_PRO_SEQ",
        sequenceName = "S912_PRO_SEQ",
              allocationSize = 1)
 @GeneratedValue(strategy = GenerationType.SEQUENCE,
        generator = "S912_PRO_SEQ")
 @Column(name = "PRO_ID", unique = true,
   nullable = false, precision = 9,
   scale = 0)
 public int getId() {
  return this.id;
 }

And using the following sequence / triggers in my DB.

CREATE SEQUENCE S912_PRO_SEQ
  nomaxvalue
  minvalue 20;

CREATE OR REPLACE TRIGGER S912_PRO_B_I_TRG 
BEFORE INSERT ON S912_project
REFERENCING OLD AS OLD NEW AS NEW 
FOR EACH ROW ENABLE
begin
IF :NEW.pro_ID IS NULL THEN
select S912_PRO_SEQ.nextval into :new.pro_ID from dual;
END IF;
end;

I was wondering if there is a way to let hibernate generate a sequence ONLY if the ID is <=0 (not set) or if the ID already exists. I know for most cases my trigger would fix the situation. But I do not want to rely completely on it.

I hope someone can help me out :p

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about sequence