简体   繁体   中英

Java - Hibernate and DB2 - Properly Sequence Usage

I was wondering how you get DB2 & Hibernate sequences to properly work?

Any input would be greatly appreciated. Below I have the sql that i did for sequence creation, the getStockId with annotation of what I think the hibernate should be for the sequence, and the error.

I created the following sequence in DB2:

  CREATE TABLE stock (
  STOCK_ID INTEGER NOT NULL,
  STOCK_CODE VARCHAR(10),
  STOCK_NAME VARCHAR(20),
  PRIMARY KEY (STOCK_ID));

  CREATE SEQUENCE seq_stock
  MINVALUE 1
  START WITH 1
  INCREMENT BY 1
  CACHE 10;

In System i I issued the following command and it worked to make sure direct SQL was going okay:

   insert into PGMR28.stock(STOCK_ID, STOCK_CODE, STOCK_NAME) VALUES(NEXT VALUE FOR PGMR28.SEQ_STOCK, 'TST','Test 123');

In my Hibernate Stock object I did the following:

@SequenceGenerator(name="SEQ_STOCK", sequenceName="SEQ_STOCK", initialValue=1, allocationSize=1)
@Id
@Column(name = "STOCK_ID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STOCK")
public Integer getStockId() {
    return this.stockId;
}

On initialValue=1 I also tried with initialValue=10. The error is at the session.getTransaction().commit(); call in my main object. I'm getting the following error:

2014-03-20_08:40:07.252 WARN  o.h.util.JDBCExceptionReporter - SQL Error: -7008, SQLState: 55019
2014-03-20_08:40:07.258 ERROR o.h.util.JDBCExceptionReporter - [SQL7008] STOCK in PGMR28 not valid for operation.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [com.mkyong.stock.Stock]
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2454)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2874)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.mkyong.App.main(App.java:37)
Caused by: java.sql.SQLException: [SQL7008] STOCK in PGMR28 not valid for operation.
    at com.ibm.as400.access.JDError.throwSQLException(JDError.java:696)
    at com.ibm.as400.access.JDError.throwSQLException(JDError.java:662)
    at com.ibm.as400.access.AS400JDBCStatement.commonExecute(AS400JDBCStatement.java:1025)
    at com.ibm.as400.access.AS400JDBCPreparedStatement.executeUpdate(AS400JDBCPreparedStatement.java:1649)
    at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2434)
    ... 11 more

This occurs because the table you are trying to update is not being journalled, and your update is being run in a non-transactional mode.

you can try this

<property name="hibernate.connection.autocommit" value="true"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM