在Hibernate中使用Annotation需要JDK 5.0的支持,Hibernate 3.2、Hibernate Annotations 3.2。
Hibernate 3.2中,hibernate3.jar是hibernate3的核心包,在lib中,antlr、dom4j、CGLIB、asm、Commons Collections、Commons Logging、 EHCache,Hibernate底层还需要Java Transaction API,所以还需要jta.jar。
Hibernate Annotations 3.2中,需要hibernate-annotations.jar、ejb3-persistence.jar。
把hibernate.cfg.xml中的映射配置改为:
- <mapping class="com.james.business.modle.domain.Event" />
之前使用xml配置内容为:
- <mapping resource="com/james/business/modle/domain/Event.hbm.xml" />
用org.hibernate.cfg.AnnotationConfiguration替代以前的org.hibernate.cfg.Configuration,来初始化org.hibernate.SessionFactory。
初始化方法,sessionFactory = new AnnotationConfiguration().configure() .buildSessionFactory();
如果这时使用sessionFactory = new Configuration().configure() .buildSessionFactory(),会报异常:An AnnotationConfiguration instance is required to use 。
参考:
http://blog.youkuaiyun.com/caterpillar_here/archive/2006/07/15/926642.aspx 林信良 Hibernate with annotation
http://www.spikesource.com/spikewatch.logs/fedora-3-i386/718/hibernate/reports/clover/org/hibernate/cfg/Configuration.html Configuration class code