在Hibernate中使用Annotation

本文介绍在Hibernate中使用注解配置的基本步骤,包括所需依赖库、配置文件修改及初始化SessionFactory的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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 代码<mapping class="com.james.business.modle.domain.Event"></mapping>
  1. <mapping class="com.james.business.modle.domain.Event" />  

之前使用xml配置内容为:

xml 代码
  1. <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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值