日期:2014-05-18  浏览次数:20830 次

hibernate 3与sql server 2005
我用的是sql server 2005 的jdbc驱动,但自增主键总有问题,错误如下:

Hibernate: insert into item_test (id, time, name) values (null, ?, ?)
11:49:58,077 WARN JDBCExceptionReporter:77 - SQL Error: 339, SQLState: S0001
11:49:58,078 ERROR JDBCExceptionReporter:78 - DEFAULT or NULL are not allowed as explicit identity values.
org.hibernate.exception.SQLGrammarException: could not insert: [events.Item]

映射方式:
<class name="events.Item" table="item_test">
  <id name="id" column="id">
  <generator class="native"/>
  </id>
  <property name="time" type="timestamp" column="time"/>
  <property name="name" type="string" column="name"/>
</class>

其中id在库表中已设为自增主键.
望高手指点.

------解决方案--------------------
你的insert语句写错了,sql server 和mysql不同,不能插入null.
改为:Hibernate: insert into item_test (time, name) values (?, ?)
就对了.