若使用注解方式创建Hibernate的持久化类,则需要在配置文件中申明持久类,如:
<mapping class="com.ca.google.model.CWord"/>
这样还是不够的,程序会报下面的错误:
Exception in thread "main" org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.ca.google.model.Word"/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1692)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1647)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1626)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1600)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
解决这一问题,必须在初始化Configuation时,使用AnnoationConfiguration,如下:
config=new AnnotationConfiguration();
sessionFactory=config.configure().buildSessionFactory();