hibernate 异常 怎么解决

2025-04-27 13:03:55
推荐回答(1个)
回答1:

1.Hibernate 常见异常
net.sf.hibernate.MappingException
当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文
件出现错误。

当出现net.sf.hibernate.MappingException: Resource: … not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中。

2. net.sf.hibernate.PropertyNotFoundException
当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property
name in class …时,原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getter或setter方法不一致。

3. org.hibernate.id.IdentifierGenerationException

当出现org.hibernate.id.IdentifierGenerationException: ids for this class
must be manually assigned before calling
save():异常时,一般是因为元素配置不正确,元素缺少其子元
的配置引起。

解决方案:元素映射了相应数据库表的主键字段,对其子元素,其中class
的取值可以为increment、identity、sequence、hilo、native……等,更多的可参考hibernate参考文档,一般取其值为native 。