I just discovered that my previous solution had a small problem. With that configuration, Hibernate issues a nextval on the sequence during insertion. This value is then discarded because the table has a trigger on insert which selects again a nextval. In this way, the ids genereated will be always even (or odd, depending on the value you start from), skipping one every other value.
In order to solve this, I have created a dummy sequence, which is called during Hibernate insertions, but the value is discarded by the table's trigger which sets the correct nextval from the correct sequence.
The mapping is then:
Code:
<hibernate-mapping package="mypackage" auto-import="false">
<import class="MyClass" rename="my" />
<class name="MyClass" table="my" lazy="false" select-before-update="true">
<id name="id" type="java.lang.String" unsaved-value="null">
<column name="ssh_id" not-null="true" />
<generator class="sequence-identity">
<param name="sequence">schema_owner.dummy_sequence</param>
</generator>
</id>
...
</class>
</hibernate-mapping>
and the table definition:
Code:
CREATE SEQUENCE dummy_sequence MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;
CREATE TABLE my (
my_id NUMBER(10) NOT NULL PRIMARY KEY,
...
);
CREATE SEQUENCE seq_my_id MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;
CREATE OR REPLACE TRIGGER trg_my_id BEFORE INSERT ON my FOR EACH ROW
BEGIN
SELECT seq_my_id.nextval INTO :new.my_id FROM DUAL;
END;
/
In this way, you can still insert into 'my' table from sqlplus or standard JDBC in the usual way, without specifying the 'my_id' value, while allowing Hibernate to work with this kind of relational model.
Hope this helps someone.
Cheers