0

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

1 Answer 1

1

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"/>
Sign up to request clarification or add additional context in comments.

3 Comments

if you want to Add table to Journal, you can look this link publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/…
@ogottwald And in the future you can use the SQL Message Finder to look up error codes.
I added Stock to the journal and it worked great and I also removed the following: , initialValue=1, allocationSize=1 in the @SequenceGenerator annotation. So you gave me what I needed. Thank you so much. Also mustaccio thank you for the SQL Message Finder. I will use that in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.