我收到此错误消息:
线程" main"中的异常 org.springframework.beans.factory.BeanCreationException:创建名称为' sessionFactory'的bean时出错在类路径资源[spring-config.xml]中定义:bean的初始化失败;嵌套异常是java.lang.NoClassDefFoundError:[Lorg / hibernate / engine / FilterDefinition;
我有一个Employee实体类
@Entity
@Table(name = "employee")
public class Employee {
private int id;
private String name;
private float salary;
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 float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
在我的spring-config.xml文件中,我没有在annotationsession工厂bean的value属性中获取类路径。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/srikanth" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>Employee</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
</value>
</property>
</bean>
<bean id="employeeDao" class="EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
这是我的主要内容:
public class InsertTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
EmployeeDao employeeDao = (EmployeeDao) context.getBean("employeeDao");
Employee e = new Employee();
e.setId(114);
e.setName("varun");
e.setSalary(50000);
employeeDao.saveEmployee(e);
}
}
答案 0 :(得分:0)
尝试编写像这样的hibernate属性:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
而不是:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
使用:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
这应该有效。