当前位置: 首页 > 知识库问答 >
问题:

SpringHibernate,无法读取xml错误

虞展
2023-03-14

我是Spring冬眠的新手,但我已经花了一天时间解决这个问题。我还是搞不清楚根本原因是什么,我该怎么办。所以,如果有人能给我一些建议,提前谢谢你。

这里有一个问题,我只是写一个简单的测试类,但是当我运行时,有一个例外:

??: 上下文初始化期间遇到异常-取消刷新

这里是类的源代码:

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(){
    }
    }

这是员工。哈佛商学院。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>

这是应用程序ontext.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();


    }

}

非常感谢你的任何建议!

共有2个答案

顾赞
2023-03-14

有时,由于编译器不知道应该使用哪个文件进行配置,所以可能无法选择您的配置文件。所以,出于这个原因,你应该通过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> 

希望它能解决你的问题

游皓
2023-03-14

属性应该在sessionFactory Bean中声明,如下所示。

           <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
           </props>

因此,您的会话工厂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>
 类似资料: