想避免在Spring 4和Hibernate 5中使用hbm.xml文件

时间:2017-05-06 07:48:15

标签: spring hibernate

当我尝试获得以下错误时:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'packageToScan' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'packageToScan' is not writable or has an invalid setter method. Did you mean 'packagesToScan'?

这里我试图在hibernateTemplate和使用Hibernate 5和Spring 4版本的帮助下连接数据库。

spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
                http://www.springframework.org/schema/beans     
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <context:component-scan base-package="com.annotations" />
    <context:component-scan base-package="com.controller" />
    <context:annotation-config />
    <mvc:annotation-driven/>

    <bean id="emp" class="com.model.Employee">
        <property name="id" value="001"/>
        <property name="name" value="Chitta"/>
        <property name="salary" value="9999"/>
    </bean>

    <bean id="dmds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
        <property name="username" value="system"></property>  
        <property name="password" value="Oracle@123"></property>
    </bean>  
    <bean id="mysessionFactory"  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dmds"></property>  

        <property name="packageToScan" value="com.model"></property>

        <property name="hibernateProperties">  
                        <props>  
                            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</prop>  
                            <prop key="hibernate.hbm2ddl.auto">create</prop>  
                            <prop key="hibernate.show_sql">true</prop>  
                        </props>  
        </property>  
    </bean>  

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="mysessionFactory"/>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">  
        <property name="sessionFactory" ref="mysessionFactory"></property>
        <!--  <property name="checkWriteOperations" value="false"></property> -->
    </bean>  

    <bean id="eDAO" class="com.dao.EmployeeDAO">  
        <property name="template" ref="hibernateTemplate"></property>  
    </bean>  

    <bean id="sevImpl" class="com.service.onlineServicesImpl">
        <property name="eDAO" ref="eDAO"></property>
    </bean> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
                        <value>/WEB-INF/pages/</value> 
        </property>
        <property name="suffix">
                        <value>.jsp</value>
        </property>
    </bean>
</beans>

以下是我在spring-dispatcher.xml文件中引用的模型类。我的代码与hbm.xml一起工作正常,但我想避免使用hbm文件。无论如何,我可以解决我的问题吗?

Employee.java

package com.model;

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

@Entity
@Table (name = "emp558", schema = "DB_USER")
public class Employee {

    @Id
    private String id;
    @Column
    private String name;
    @Column
    private String salary;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

}

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#FFFFFF</color> <color name="yellow">#FFFF00</color> <color name="fuchsia">#FF00FF</color> <color name="red">#FF0000</color> <color name="silver">#C0C0C0</color> <color name="gray">#808080</color> <color name="olive">#808000</color> <color name="purple">#800080</color> <color name="maroon">#800000</color> <color name="aqua">#00FFFF</color> <color name="lime">#00FF00</color> <color name="teal">#008080</color> <color name="green">#008000</color> <color name="blue">#0000FF</color> <color name="navy">#000080</color> <color name="black">#000000</color> </resources> 替换为  <property name="packageToScan" value="com.model">中的<property name="packagesToScan" value="com.model">  文件