JSF 2.0 + Spring + Hibernate integration(续)

本文介绍如何在JSF+Spring+Hibernate项目中使用JPA注解替代XML配置,简化实体映射文件的编写。展示了如何修改SessionFactory配置,并在实体类中添加必要的JPA注解。

在使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值