-->
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.  [ 2 posts ] 
Author Message
 Post subject: problem with generator classes
PostPosted: Sat Jan 15, 2005 7:17 am 
Newbie

Joined: Thu Jan 13, 2005 3:11 am
Posts: 11
I can retrieve data from MySql database but just can't save object into mySql database.

a usual pojo java bean with a usual mapping file, the id element was:
Code:
    <id name="userID" column="userID">
      <generator class="native"/>
    </id>


when i save oject, the error was

Code:
com.element.photohost.exception.InfrastructureException: net.sf.hibernate.HibernateException: The database returned no natively generated identity value
   at com.element.photohost.business.dao.imp1.UserDaoImp1.saveUser(UserDaoImp1.java:69)
   at com.element.photohost.test.TestUserDaoImp1.testSaveUser(TestUserDaoImp1.java:44)
...(Click for full stack trace)...
Caused by: net.sf.hibernate.HibernateException: The database returned no natively generated identity value
   at net.sf.hibernate.persister.AbstractEntityPersister.getGeneratedIdentity(AbstractEntityPersister.java:1230)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:530)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
   at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
   at com.element.photohost.business.dao.imp1.UserDaoImp1.saveUser(UserDaoImp1.java:67)
   ... 16 more


then i changed the id element like this:

error message was:
Code:
    <id name="userID" column="userID">
      <generator class="identity"/>
    </id>

com.element.photohost.exception.InfrastructureException: net.sf.hibernate.HibernateException: The database returned no natively generated identity value


then i tried sequence generator, encountered error is:
Caused by: net.sf.hibernate.MappingException: could not instantiate id generator
at net.sf.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:82)
at net.sf.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:82)
at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:635)
at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:716)
at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:42)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:136)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:791)
at com.element.photohost.utils.HibernateUtil.<clinit>(HibernateUtil.java:36)
... 12 more
Caused by: net.sf.hibernate.MappingException: Dialect does not support sequences
at net.sf.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:319)
at net.sf.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:62)
at net.sf.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:78)
... 19 more


finally, i changed to hilo generator, the problem is:
Caused by: net.sf.hibernate.JDBCException: Could not save object
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:783)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at com.element.photohost.business.dao.imp1.UserDaoImp1.saveUser(UserDaoImp1.java:67)
... 16 more
Caused by: java.sql.SQLException: General error message from server: "Table 'photobank.hibernate_unique_key' doesn't exist"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1586)
at com.mchange.v2.sql.filter.FilterPreparedStatement.executeQuery(FilterPreparedStatement.java:68)
at com.mchange.v2.c3p0.impl.C3P0PooledConnection$2.executeQuery(C3P0PooledConnection.java:567)
at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:93)
at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765)
... 19 more

I created a hibernate_unique_key table:
Code:
CREATE TABLE IF NOT EXISTS hibernate_unique_key(
  next_hi INT
)TYPE=InnoDB;


this time, prompt error message is:
Caused by: net.sf.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_unique_key
at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:97)
at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at com.element.photohost.business.dao.imp1.UserDaoImp1.saveUser(UserDaoImp1.java:67)
... 16 more


what's the problem with my program

btw: I can retrieve data from MySql via Hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 17, 2005 1:17 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
I'd go back to using "native" but verify that the column is indeed an "Auto Increment" column. It sounds to me like the column was never marked as an "auto increment", but was simply an INT set as the primary key of your table.

BTW, according to the reference guide, generator type "sequence" does not apply to MySQL:

http://www.hibernate.org/hib_docs/refer ... -generator

You'll want to use "identity" or "native" which for MySQL will result in exactly the same thing. Also, if my memory serves me correctly, the auto increment feature doesn't exist in all the versions of MySQL (I could be wrong here...) so check your version of that as well, just to be sure. Then, run some tests in the DB itself to make sure that the identity (auto increment) column is indeed incrementing when you insert.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.