当前位置: 代码迷 >> J2EE >> 但是他报错: org.hibernate.id.IdentifierGenerationException: ids for this
  详细解决方案

但是他报错: org.hibernate.id.IdentifierGenerationException: ids for this

热度:247   发布时间:2016-04-22 03:34:34.0
通过Hibernate向数据库里插数据,数据库里的自动增长列怎么办
我有一张表
CREATE TABLE `isread` (
  `RID` int(11) NOT NULL auto_increment,
  `WID` int(11) NOT NULL,
  `leader` int(11) NOT NULL,
  PRIMARY KEY (`RID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Rid 为自动增长列
我写的代码为:
public void InsertIsread(Integer WID, Integer EID) {
try {
Session session = factory.openSession();
Transaction tran = session.beginTransaction();
IsRead read = new IsRead();
read.setLeader(EID);
read.setWid(WID);
session.save(read);
tran.commit();
session.close();
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
但是他报错: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): IsRead

要怎么插入才正确呢

------解决方案--------------------
主键生成策略采用identity
<id name="id" column="RID" type="int">
<generator class="identity" />
</id>
------解决方案--------------------
increment
------解决方案--------------------
<id name="id" column="id" unsaved-value="-1"></id> 
主键 这样配置也可以
  相关解决方案