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