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

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>  

 

posted @ 2013-04-24 00:06  Pan Code  阅读(2847)  评论(0编辑  收藏  举报
88CTO版权所有