JSF 2.0 + Spring + Hibernate integration(续)

系统 1650 0

在使用jsf+Spring+Hibernate做项目时,发现配置Hibernate的实体映射文件相当繁琐.前段时间做EJB时,一直采用的是JPA的注解方式.相比较之下,少写不少代码.于是花了些时间.将项目中原来使用xml配置的方式转成使用Annotation方式.记录如下:

      
        <!--
      
      
         Hibernate session factory 
      
      
        -->
      
      
        <!--
      
      
         For using no-Annotation 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

    
      
      
        -->
      
      
        <!--
      
      
         using for Annotation 
      
      
        -->
      
      
        <
      
      
        bean 
      
      
        id
      
      
        ="sessionFactory"
      
      
        

        class
      
      
        ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      
      
        >
      
    

注:为了使用Annotation,需将原HibernateSessionFactory.xml中sessionFactory的实现类改成:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

      
        <!--
      
      
         

        <property name="mappingResources"> 

            <list> 

                <value>com/singtel/config/hibernate/Customer.hbm.xml </value>

            </list> 

        </property> 

        
      
      
        -->
      
      
        <!--
      
      
         Using for Annotation 
      
      
        -->
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="annotatedClasses"
      
      
        >
      
      
        <
      
      
        list
      
      
        >
      
      
        <
      
      
        value
      
      
        >
      
      com.singtel.system.model.Customer
      
        </
      
      
        value
      
      
        >
      
      
        </
      
      
        list
      
      
        >
      
      
        </
      
      
        property
      
      
        >
      
    

注:原来使用mappingResources来配对hbm.xml文件,现用annotatedClasses来直接映射到指定Class.

Or:也可通过通配符来自动扫描类包

      
        <
      
      
        property 
      
      
        name
      
      
        ="packagesToScan"
      
      
        >
      
      
        <
      
      
        list
      
      
        >
      
      
        <
      
      
        value
      
      
        >
      
      com.singtel.system.model.*
      
        </
      
      
        value
      
      
        >
      
      
        </
      
      
        list
      
      
        >
      
      
        </
      
      
        property
      
      
        >
      
    

注:packagesToScan是Spring 2.5.6新特性(推荐)

接下来要做的就是在java实体中增加注解.

      
        package
      
      
         com.singtel.system.model;




      
      
        import
      
      
         java.io.Serializable;


      
      
        import
      
      
         java.util.Date;




      
      
        import
      
      
         javax.persistence.Column;


      
      
        import
      
      
         javax.persistence.Entity;


      
      
        import
      
      
         javax.persistence.GeneratedValue;


      
      
        import
      
      
         javax.persistence.GenerationType;


      
      
        import
      
      
         javax.persistence.Id;


      
      
        import
      
      
         javax.persistence.Table;


      
      
        import
      
      
         javax.persistence.Temporal;


      
      
        import
      
      
         javax.persistence.TemporalType;



@Entity(name
      
      ="Customer"
      
        )

@Table(name
      
      ="CUSTOMER_LWC"
      
        )


      
      
        public
      
      
        class
      
       Customer 
      
        implements
      
      
         Serializable{

    @Id

    @Column(name
      
      ="CUSTOMER_ID",columnDefinition = "Integer"
      
        )

    @GeneratedValue(strategy 
      
      =
      
         GenerationType.AUTO)

    
      
      
        public
      
      
        long
      
      
         customerId;

    

    @Column(name
      
      ="CUSTOMER_ADDRESS",columnDefinition = "varchar2(255)", nullable = 
      
        false
      
      
        )

    
      
      
        public
      
      
         String address;

    

    @Column(name
      
      ="CUSTOMER_PASSWORD",columnDefinition = "varchar2(45)", nullable = 
      
        false
      
      
        )

    
      
      
        public
      
      
         String password;

    

    @Column(name 
      
      = "CREATED_DATE", nullable = 
      
        false
      
      
        )

    @Temporal(TemporalType.TIMESTAMP)

    
      
      
        public
      
      
         Date createdDate;

    

    
      
      
        public
      
      
        long
      
      
         getCustomerId() {

        
      
      
        return
      
      
         customerId;

    }

    
      
      
        public
      
      
        void
      
       setCustomerId(
      
        long
      
      
         customerId) {

        
      
      
        this
      
      .customerId =
      
         customerId;

    }

    
      
      
        public
      
      
         String getPassword() {

        
      
      
        return
      
      
         password;

    }

    
      
      
        public
      
      
        void
      
      
         setPassword(String password) {

        
      
      
        this
      
      .password =
      
         password;

    }

    
      
      
        public
      
      
         String getAddress() {

        
      
      
        return
      
      
         address;

    }

    
      
      
        public
      
      
        void
      
      
         setAddress(String address) {

        
      
      
        this
      
      .address =
      
         address;

    }

    
      
      
        public
      
      
         Date getCreatedDate() {

        
      
      
        return
      
      
         createdDate;

    }

    
      
      
        public
      
      
        void
      
      
         setCreatedDate(Date createdDate) {

        
      
      
        this
      
      .createdDate =
      
         createdDate;

    }

    

}
      
    

删除原有hbm.xml.Ok

JSF 2.0 + Spring + Hibernate integration(续)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论