我是Spring + Hibernate的新手,但我已经解决了这个问题。我仍然无法弄清楚根本原因是什么,我该怎么办。所以,如果有人能给我一些建议,请提前感谢你。
这是一个问题,我只是编写一个简单的测试类,但是当我运行时,有一个例外:
??:在上下文初始化期间遇到异常 - 取消刷新>尝试:org.springframework.beans.factory.BeanCreationException:错误>创建名为'sessionFactory'的bean在类路径资源中定义>>>> ;>>> [ApplicationContext.xml]:调用init方法失败;嵌套异常是> org.hibernate.InvalidMappingException:无法读取XML 线程“main”中的异常> org.springframework.beans.factory.BeanCreationException:在类路径资源中定义了名称为'sessionFactory'的bean>时出错> [ApplicationContext.xml]:init方法的调用失败;嵌套异常是> org.hibernate.InvalidMappingException:无法读取XML 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139) 在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83) 在com.xw.test.Test.main(Test.java:30)
这是类源代码:
package com.domain;
import java.util.Date;
public class Employee {
private Integer id;
private String name;
private String email;
private java.util.Date hiredate;
private Float salary;
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public java.util.Date getHiredate() {
return hiredate;
}
public void setHiredate(java.util.Date hiredate) {
this.hiredate = hiredate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Employee( String name, String email, Date hiredate,
Float salary) {
this.name = name;
this.email = email;
this.hiredate = hiredate;
this.salary = salary;
}
public Employee(){
}
}
这是Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-mapping package="com.domain">
<class name="Employee" table="employee">
<id name="id" type="java.lang.Integer">
<generator class="native" ></generator>
</id>
<property name="email" type="java.lang.String" >
<column name="email" length="64"/>
</property>
<property name="hiredate" type="java.util.Date">
<column name="hiredate" />
</property>
<property name="name" type="java.lang.String">
<column name="name"/>
</property>
<property name="salary" type="java.lang.Float">
<column name="salary"/>
</property>
</class>
</hibernate-mapping>
这是ApplicationContext.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="testService" class="com.xw.test.TestService">
<property name="name" value="XingWang"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="111111"/>
<property name="initialSize" value="3"/>
<property name="maxActive" value="500"/>
<property name="maxIdle" value="2"/>
<property name="minIdle" value="1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/domain/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect
</value>
</property>
</bean>
</beans>
和pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myssh</groupId>
<artifactId>myssh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebRoot</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.2.Final</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
这是smiple测试类的源代码:
/**
*
*/
package com.xw.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.domain.Employee;
/**
* @author Administrator
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
SessionFactory sf = (SessionFactory) ac.getBean("sessionFactory");
Session s = sf.openSession();
Employee employee = new Employee( "aa", "aa.sohu@com", new java.util.Date(),
(float) 234.56) ;
Transaction tx = s.beginTransaction();
s.save(employee);
tx.commit();
}
}
非常感谢您的任何建议!
答案 0 :(得分:0)
应该在sessionFactory Bean中声明属性如下所示。
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
</props>
所以你的sessionFactory bean就像下面一样。
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/domain/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
</props>
</property>
答案 1 :(得分:0)
有时可能无法选择您的配置文件,因为编译器不知道配置时需要使用哪个文件。因此,出于这个原因,您应该通过2种方法显式导入那些配置文件,如我所知 1)通过使用类
@Configuration
@ImportResource(value = {"classpath:{filename-within-resource}/Employee.hbm.xml"},"classpath:{filename-within-resource}/ApplicationContext.cfg.xml")
public class CommonSolutionConfig {
}
2)通过使用xml
<?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.1.xsd">
<import resource="classpath:{filename-within-resource}/Employee.hbm.xml"/>
<import resource="classpath:{filename-within-resource}/ApplicationContext.cfg.xml"/>
</beans>
希望它将解决您的问题