Hi,
There is something I don't understand regarding the generation of the id by using a sequence in Oracle.
Here is my sequence:
Code:
SQL> SELECT SEQ_INSTRUMENT.nextval FROM DUAL;
NEXTVAL
----------
1
My Pojo definition, generated by Hibernate Tool:
Code:
/**
* Instrument generated by hbm2java
*/
@Entity
@Table(name="INSTRUMENT"
, uniqueConstraints = { @UniqueConstraint( columnNames = { "CODE" } ) })
public class Instrument implements java.io.Serializable {
....
@Id( generate=GeneratorType.SEQUENCE, generator="SEQ_INSTRUMENT_LOG")
@SequenceGenerator(name="SEQ_INSTRUMENT_LOG", sequenceName="SEQ_INSTRUMENT")
@Column(name="ID", unique=true, nullable=false, insertable=true, updatable=true, precision=18, scale=0)
public long getId() {
return this.id;
}
And my service which persists the Pojo:
Code:
@Stateless
@RemoteBinding(jndiBinding="hedgefundpro/remote/InstrumentService")
@LocalBinding(jndiBinding="hedgefundpro/local/InstrumentService")
public class InstrumentService implements IInstrument, Serializable {
@PersistenceContext(unitName="HedgeFundPro")
protected EntityManager em;
public InstrumentService() {
super();
}
public Instrument add(Instrument instrument) {
em.persist(instrument);
return instrument;
}
When I look at the logs, there are no statement to retrieve the id from the sequence.
The logs:
Code:
21:04:16,390 INFO [STDOUT] Hibernate: select instrument_.ID, instrument_.SHORT_NAME as SHORT2_237_, instrument_.LONG_NAME as LONG3_237_ from INSTRUMENT_TYPE instrument_ where instrument_.ID=?
21:04:16,396 INFO [STDOUT] Hibernate: insert into INSTRUMENT (NAME, CODE, PARENT, INSTRUMENT_TYPE, BASE_CURRENCY, UNDERLYING_CURRENCY, ID) values (?, ?, ?, ?, ?, ?, ?)
21:04:16,405 WARN [JDBCExceptionReporter] SQL Error: 1, SQLState: 23000
21:04:16,405 ERROR [JDBCExceptionReporter] ORA-00001: unique constraint (MY_SCHEMA.SYS_C009217) violated
I could use the following workaround, but I would appreciate to understand my mistake and benefits from every bit of Hibernate...
Code:
...
Long id = (Long)em.createNativeQuery("SELECT SEQ_INSTRUMENT.nextval FROM DUAL").getSingleResult();
instrument.setId(id);
...
Can someone tell me what I'm missing or did wrong?
Best regards.