我是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种方法显式导入这些配置文件,我知道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>
希望它能解决你的问题
属性应该在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>
我正在获取O并且从未进行成功的Hibernate连接测试,在学习了本教程“JasperReports with hibernate-module1”和JasperReports with hibernate-module2之后,弹出一个错误,说“Could not parse mapping document from resource com/report/mappings/department
我需要你帮我把冬眠映射成一对多我不知道为什么这是错的 另一类: 映射 另一个映射: Hibernate配置 这就是错误: 如果有人能帮我,我非常感激,谢谢!
我试图读取一个特点后,关贸总协定连接和服务发现成功。但接收错误15(0x0f,GATT_INSUFFICIENT_ENCRYPTION),然后137(0x0089)在gatt回调。在此错误之后,gatt立即断开连接。 我的设备是三星S4,4.4.2。
我想在我的角度项目中添加钢系列。我有createad html-compass组件,但我收到了这个错误: _services错误类型错误:无法读取未定义的属性(读取主题)在ProjectService…/app/Subject.js:53/_next(node_modules)在_esm5__tryOrUnsub[EventEmitter.push.](node_modules)在_esm5…/c
我收到错误: 错误类型错误: 无法读取在评估 (webpack-internal:///./node_modules/@angular/common/esm5/http.js:163) 处未定义的属性 “长度”在 Array.forEach () 在 httpHeaders.lazyInit (webpack-internal:///./node_modules/@angular/common/e