---
title: "在Hibernate中使用Annotation"
author: "Jameslyy"
date: "2007-09-29T10:09:18Z"
tags: [".net", "Blog", "Hibernate", "JDK", "XML"]
slug: "hibernate-annotation"
draft: false
lastmod: "2007-09-29T10:09:18Z"
id: 128108
url: "https://jamesblog.iteye.com/blog/128108"
---

在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中的映射配置改为：

xml 代码&lt;mapping class="com.james.business.modle.domain.Event"&gt;&lt;/mapping&gt;

1. &lt;mapping class="com.james.business.modle.domain.Event" /&gt;

之前使用xml配置内容为:

xml 代码

1. &lt;mapping resource="com/james/business/modle/domain/Event.hbm.xml" /&gt;

用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.csdn.net/caterpillar\_here/archive/2006/07/15/926642.aspx](http://blog.csdn.net/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](http://www.spikesource.com/spikewatch.logs/fedora-3-i386/718/hibernate/reports/clover/org/hibernate/cfg/Configuration.html)  Configuration class code