-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Cannot insert a record with Id <generator> to the data
PostPosted: Mon Feb 09, 2009 10:42 pm 
Newbie

Joined: Mon Feb 09, 2009 10:26 pm
Posts: 2
Hi,

I am relatively new to Hibernate, struggling with inserting records to database because Id is generated with "increment" class. This is mapping is working fine with select, updates and delete.

Here my mappings and associated classes.

Mapping file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false">
   <class name="com.ss.dao.hibernate.BaseRecord" table="TMPSCHM.TSTPE_BASE">
      <id name="id" column="STATBASE_ID" unsaved-value="0">
         <generator class="increment"/>
      </id>   
      <property name="linkMapID" type="java.lang.Integer"
         column="LNKMAP_ID" not-null="false"/>
      <property name="refID" type="java.lang.Integer"
         column="REF_ID" not-null="false"/>      
      <property name="controllerID" type="java.lang.Integer"
         column="CONTROLLER_ID" not-null="false"/>
   </class>
</hibernate-mapping>




Class File:

Code:
public class BaseRecord{
   
   private Long id;
   private Integer linkMapID;
   private Integer refID;
   private Integer controllerID;

   /**
    * @param long
    */
   public void setId(Long id) {
      this.id = id;
   }

   /**
    * @return
    */
   public Long getId() {
      return id;
   }


   /**
    * @return
    */
   public Integer getLinkMapID() {
      return linkMapID;
   }

   /**
    * @return
    */
   public Integer getRefID() {
      return refID;
   }

   /**
    * @param integer
    */
   public void setLinkMapID(Integer integer) {
      linkMapID = integer;
   }

   /**
    * @param integer
    */
   public void setRefID(Integer integer) {
      refID = integer;
   }   
   
   /**
    * @param integer
    */
   public void setControllerID(Integer controllerID) {
      this.controllerID = controllerID;
   }

   /**
    * @return
    */
   public Integer getControllerID() {
      return controllerID;
   }

}



Class I am using to insert data to "TMPSCHM.TSTPE_BASE" table. Please note I removed method "readBaseData(String filename, int worksheet)"
from the class "ReadBaseDataFromExcel", since it is a very big method and it's working fine with out any issues.

Code:
public class ReadBaseDataFromExcel {
   
   /** Logger for this class and subclasses */
   protected final Log log = LogFactory.getLog(getClass());
   
   
   public List readBaseData(String filename, String location, int worksheet) {
      List list = null;
      filename =  location.concat(filename);
      list = readBaseData(filename, worksheet);
      return list;
   }

   public static void main(String args[]){
      ReadBaseDataFromExcel rsb = new ReadBaseDataFromExcel();
      List list = rsb.readBaseData("C:\\Documents and Settings\\Uz1iv8q\\My Documents\\STP Data\\People Safe\\Status_SSC.xls", 0);
      BaseRecordDaoImpl brDaoImpl = (BaseRecordDaoImpl)new BeanFinder().getBean("BaseRecordDaoTarget");
      Iterator it = list.iterator();
      while(it.hasNext()) {
         BaseRecord br = (BaseRecord)it.next();
         try {
            brDaoImpl.saveOrUpdate(bsr);
         } catch (DataAccessException e) {
            System.out.println("DataAccessException: " + e.getMessage());
         } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
         }
      }      
   }

}



When I tried to insert records to the table I am getting following error. I know, you experts can fix this in minutes. Please help me to fix my error immediately since I am supposed give demo on application tomorrow evening.

Error:

Code:
Hibernate: select max(STATBASE_ID) from TMPSCHM.TSTPE_STATBASE
Hibernate: insert into TMPSCHM.TSTPE_STATBASE (LNKMAP_ID, REF_ID, CONTROLLER_ID, STATBASE_ID) values (?, ?, ?, ?)

2009-02-09 19:13:49,968 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: -798, SQLState: 428C9>
2009-02-09 19:13:49,968 ERROR [org.hibernate.util.JDBCExceptionReporter] - <DB2 SQL error: SQLCODE: -798, SQLSTATE: 428C9, SQLERRMC: STATBASE_ID>
2009-02-09 19:13:49,968 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: -727, SQLState: 56098>
2009-02-09 19:13:49,968 ERROR [org.hibernate.util.JDBCExceptionReporter] - <DB2 SQL error: SQLCODE: -727, SQLSTATE: 56098, SQLERRMC: 2;-798;428C9;STATBASE_ID>
2009-02-09 19:13:49,968 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: -727, SQLState: 56098>
2009-02-09 19:13:49,968 ERROR [org.hibernate.util.JDBCExceptionReporter] - <DB2 SQL error: SQLCODE: -727, SQLSTATE: 56098, SQLERRMC: 2;-798;428C9;STATBASE_ID>
2009-02-09 19:13:49,978 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - <Could not synchronize database state with session>
org.hibernate.exception.SQLGrammarException: could not insert: [com.ss.dao.hibernate.BaseRecord]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2272)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665)
   at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:375)
   at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:699)
   at com.ss.dao.BaseRecordDaoImpl.saveOrUpdate(BaseRecordDaoImpl.java:61)
   at com.ss.service.ReadStatBaseDataFromExcel.main(ReadStatBaseDataFromExcel.java:328)
Caused by: com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -798, SQLSTATE: 428C9, SQLERRMC: STATBASE_ID
   at com.ibm.db2.jcc.c.yc.e(yc.java:1512)
   at com.ibm.db2.jcc.c.yc.a(yc.java:1112)
   at com.ibm.db2.jcc.a.db.h(db.java:148)
   at com.ibm.db2.jcc.a.db.a(db.java:42)
   at com.ibm.db2.jcc.a.r.a(r.java:31)
   at com.ibm.db2.jcc.a.sb.g(sb.java:149)
   at com.ibm.db2.jcc.c.yc.l(yc.java:1092)
   at com.ibm.db2.jcc.c.zc.ab(zc.java:1795)
   at com.ibm.db2.jcc.c.zc.d(zc.java:2237)
   at com.ibm.db2.jcc.c.zc.S(zc.java:557)
   at com.ibm.db2.jcc.c.zc.executeUpdate(zc.java:540)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252)
   ... 13 more
DataAccessException: could not insert: [com.ss.dao.hibernate.BaseRecord]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.ss.dao.hibernate.BaseRecord]



Thanks in advance. Your quick response and help is greatly appreciated.

rchitela



Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 2:34 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I think you generator should be
<generator class="identity"/> and not increment.

Also check that your hibernate.dialect is set to org.hibernate.dialect.DB2Dialect.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Thanks, It working now.
PostPosted: Tue Feb 10, 2009 1:58 pm 
Newbie

Joined: Mon Feb 09, 2009 10:26 pm
Posts: 2
Thanks "littypreethkr". It's working now.

I went through Hibernate mapping section. I forgot to use this option. I tried all other options. No wonder my friends think I am stupid. I should probably go to eye doctor. Anyway thanks for the help.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.