日期:2014-05-20  浏览次数:20904 次

hibernate主键生成策略native问题
我用的是mysql数据库,生成策略是increment时一切正常,但如果换做native就老说插入语句主键不能为空
xml配置:
<hibernate-mapping package="domain">
<class name="Student" table="Student">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="birthday"/>
<one-to-one name="idCard"/>
<many-to-one name="department" column="department_id" not-null="true" />
</class>
</hibernate-mapping>
错误提示:
Hibernate: insert into Student (name, birthday, department_id, id) values (?, ?, ?, ?)
2011-11-27 10:22:48 org.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 0, SQLState: 07001
2011-11-27 10:22:48 org.hibernate.util.JDBCExceptionReporter logExceptions
严重: No value specified for parameter 4
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [domain.Student]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at dao.StudentDAO.add(StudentDAO.java:25)
at test.Test.main(Test.java:38)
Caused by: java.sql.SQLException: No value specified for parameter 4
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2176)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1993)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 17 more
数据库表结构:
 student | CREATE TABLE `student` (
 `id` int(11) NOT NULL auto_increment,
 `name` varchar(255) default NULL,
 `birthday` datetime default NULL,