欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate annotation简单配置

程序员文章站 2022-04-17 10:49:33
...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>WEB-INF/config/datasource.properties</value>
            </list>
        </property>
    </bean>

    <context:component-scan base-package="com.test.hrrs" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate.statistics}</prop>
                <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
                <prop key="connection.useUnicode">${hibernate.connection.useUnicode}</prop>
                <prop key="connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
                <prop key="hibernate.autoReconnect">${hibernate.autoReconnect}</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.test.hrrs.entity.user.User</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

 

/**
 * 
 */
package com.test.hrrs.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

/**
 * @author Clay Zhong, [email protected]
 * @date May 9, 2009
 */
@MappedSuperclass
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = -7551458114675415168L;

    protected String id;
    protected Date createdDate;
    protected Date updatedDate;
    protected Date expiredDate;

    public boolean equals(Object obj) {
        if (obj instanceof BaseEntity) {
            return ((BaseEntity) obj).getId().equals(id);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return id.hashCode() * 17;
    }

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "created_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @Column(name = "updated_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    @Column(name = "expired_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getExpiredDate() {
        return expiredDate;
    }

    public void setExpiredDate(Date expiredDate) {
        this.expiredDate = expiredDate;
    }
}

 

/**
 * 
 */
package com.test.hrrs.entity.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.test.hrrs.entity.BaseEntity;

/**
 * @author Clay Zhong, [email protected]
 *
 * @date Jun 20, 2009
 */
@Entity
@Table(name = "user")
public class User extends BaseEntity {
    private static final long serialVersionUID = -8755593241668230921L;

    private String username;
    private String password;
    private String email;

    public String toString() {
        return new StringBuilder("[User Entity] id: ").append(id).append(", name: ").append(username).toString();
    }

    @Column(nullable = false, length = 45, unique = true)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(nullable = false, length = 45)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(nullable = false, length = 100, unique = true)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}