日期:2014-05-19  浏览次数:20839 次

hibernate 实体类使用注解后,如何在spring applicationContext.xml中配置
大家的hibernate 和spring整合时的配置文件一般都怎么写配置的。
我的hibernate 实体类使用注解配置,在applicationContext.xml中配置后,数据库中木有自动生成映射表啊。搜了一大圈,都不好使,木有经验,请指点。
我的实体类:
Java code

package domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 分类(顶级版面)
 * @author wn
 *
 */

@Entity
@Table(name="t_category")
public class Category {
    
    private int id;
    private String name;
    private int order;
    
    /**
     * 分类(顶级版面)中的子版面(二级版面)
     * 1-N关联关系,用Set来保存关联实体
     */
//    private Set<Forum> forums=new HashSet<Forum>();
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
//    public Set<Forum> getForums() {
//        return forums;
//    }
//    public void setForums(Set<Forum> forums) {
//        this.forums = forums;
//    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Category other = (Category) obj;
        if (id != other.id)
            return false;
        return true;
    }
    
    
    
    

}




我的hibernate.cfg.xml 
XML code

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
                       
    </session-factory>
</hibernate-configuration>



我的applicationContext.xml
XML code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    
    <!-- more bean definitions for data access objects go here -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/mybbs2"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
    <property name="maxActive" value="100"></property>
    <property name="maxIdle" value="30"></property>
    <property name="maxWait" value="500"></property>