手记

Hibernate注解时常见错误

异常:

org.hibernate.MappingException:
An AnnotationConfiguration instance is required to use <mapping class="com.xxxchina.instance.xxxContext"/>

错误原因:
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();

解决方法:

Hibernate配置文件中,若带有<mapping class="com.xxx.Dog"/>,则说明映射类时,采用了Annotation方式。在初始化Configuation时,应使用AnnoationConfiguration,代码如下:
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();

2人推荐
随时随地看视频
慕课网APP