首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate 实体类使用注解后,怎么在spring applicationContext.xml中配置

2012-05-28 
hibernate 实体类使用注解后,如何在spring applicationContext.xml中配置大家的hibernate 和spring整合时

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>    <!--      <property name="defaultAutoCommit" value="true"></property>    -->    </bean>    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>        <property name="packagesToScan">               <list>                    <value>com.jframe.*</value>               </list>         </property>        <!--  这种方法没起作用        <property name="annotatedClasses">            <list>                <value>domain.Category</value>            </list>        </property>        -->    </bean>             <bean id="categoryDao" class="dao.impl.CategoryDaoImpl" scope="singleton">        <property name="sessionFactory">            <ref bean="sessionFactory"/>        </property>    </bean>    <bean id="categoryService" class="service.impl.CategoryServiceImpl">        <property name="categoryDao" ref="categoryDao"></property>    </bean>    <bean id="categoryAction" class="action.CategoryAction" scope="prototype">        <property name="categoryService" ref="categoryService"></property>    </bean>    </beans> 


hibernate 3.6.0 struts2 2.2 spring 3.0.5
tomcat6启动时,也没有报错,warn都木有。请有经验者指点






[解决办法]
注解和ApplicationContext配置文件逐个配置bean效果是一样的,都是在context中管理,只是注解使用的是反射原理来实例化的。实体bean注解的话在外面的应用层建议也使用注解调用。当然注解也需要在context进行扫描的。

热点排行