hibernate annotation(一)

1.概述
     值得期待的Hibernate Annotation 式配置终于随着Hibernate 3.2GA 版本的发布而宣布正式被支持了!
     只要数据库以及字段名称设计合适,我们甚至只需要在原来程序上加上3行代码,就可以配置完成一个Bean。这依     稀看到了Rails 的影子......

1.1 参考资料
  最为重要的参考资料莫过于由RedSega 团队翻译的,并入Hibernate 官方文档库的参考手册中文版。

2.系统配置
   1. 首先,需要将hibernate-annotations 的相关jar 包(hibernate-annotations.jar,hibernate-commons-   annotations.jar,ejb3-persistence.jar)引入项目。
   2. 可以使用hibernate.cfg.xml ,也可以使用Spring的封装来配置annotation pojo

     a. 直接在spring中配置  sessionFactory的类型改为  org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

    增加如下节点配置
     <property name="annotatedClasses">   
          <list><value>org.springside.helloworld.model.User</value></list>
     </property>


    b. 在hibernate.cfg.xml 中配置
      创建hibernate.cfg.xml 文件,以SpringSide 项目的HelloWorld 为例,写入以下内容

<!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
   <hibernate-configuration>
        <session-factory> 
             <mapping class="org.springside.helloworld.model.User"/>
        </session-factory>
   </hibernate-configuration>

   同时,在Spring 配置文件中,将原来的SessionFactory 配置改为:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocations">
             <list><value>classpath*:/config/hibernate.cfg.xml</value></list>
        </property>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值