Hibernate报错:org.hibernate.id.IdentifierGenerationException:ids for this class must be manually assig

来源:互联网 发布:mysql 两个表的合集 编辑:程序博客网 时间:2024/05/16 05:01

org.hibernate.id.IdentifierGenerationException:ids for this class must be manually assigned before calling save()

hibernate出现这个错误的原因有可能因为,你的表中有个主键。 但是你插入的(调用save)时那个值是null

你要操作的数据表中的id(即主键)的类型设置成了“自动增长类型”,而在你的

hibernate.cfg.xml中,id的生成方式是assigned,即

 

[xhtml] view plaincopy
  1. <id name="id" type="integer">  
  2.     <column name="id" />  
  3.     <generator class="assigned" />  
  4. </id>  

 

解决方法:

把主键的生成方式改为native,它的特征是能够根据底层数据库自动选择主键生成方式

[xhtml] view plaincopy
  1. <id name="id" type="integer">  
  2.     <column name="id" />  
  3.     <generator class="native" />  
  4. </id>