当我试图得到如下错误:想要避免使用hbm.xml文件中的使用Spring 4和Hibernate 5
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="[email protected]"></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;
}
}
如果您为什么要使用XML文件在所有使用Hibernate 5和Spring 4?您可以单独使用Java代码完成所有任务!另外如果你使用Spring引导,你可以节省很多Boilerplate数据源/ JPA设置。 –