[英]Problems using HibernateTemplate: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
I am quite new in Spring world and I am going crazy trying to integrate Hibernate in Spring application using HibernateTemplate abstract support class 我在Spring世界中还很陌生,我想通过使用HibernateTemplate抽象支持类将Hibernate集成到Spring应用程序中而感到疯狂。
I have the following class to persist on database table: 我有以下类别可保留在数据库表上:
package org.andrea.myexample.HibernateOnSpring.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="person")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int pid;
private String firstname;
private String lastname;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
Next to it I have create an interface named PersonDAO in wich I only define my CRUD method. 在它旁边,我只创建了一个名为PersonDAO的接口,仅定义了我的CRUD方法。 So I have implement this interface by a class named PersonDAOImpl that also extend the Spring abstract class HibernateTemplate :
因此,我已经通过名为PersonDAOImpl的类实现了此接口,该类还扩展了Spring抽象类HibernateTemplate :
package org.andrea.myexample.HibernateOnSpring.dao;
import java.util.List;
import org.andrea.myexample.HibernateOnSpring.entity.Person;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PersonDAOImpl extends HibernateDaoSupport implements PersonDAO{
public void addPerson(Person p) {
getHibernateTemplate().saveOrUpdate(p);
}
public Person getById(int id) {
// TODO Auto-generated method stub
return null;
}
public List<Person> getPersonsList() {
// TODO Auto-generated method stub
return null;
}
public void delete(int id) {
// TODO Auto-generated method stub
}
public void update(Person person) {
// TODO Auto-generated method stub
}
}
(at the moment I am trying to implement only the addPerson() method) (目前,我仅尝试实现addPerson()方法)
Then I have create a main class to test the operation of insert a new object into the database table: 然后,我创建了一个主类来测试将新对象插入数据库表的操作:
package org.andrea.myexample.HibernateOnSpring;
import org.andrea.myexample.HibernateOnSpring.dao.PersonDAO;
import org.andrea.myexample.HibernateOnSpring.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
System.out.println("Contesto recuperato: " + context);
Person persona1 = new Person();
persona1.setFirstname("Pippo");
persona1.setLastname("Blabla");
System.out.println("Creato persona1: " + persona1);
PersonDAO dao = (PersonDAO) context.getBean("personDAOImpl");
System.out.println("Creato dao object: " + dao);
dao.addPerson(persona1);
System.out.println("persona1 salvata nel database");
}
}
As you can see the PersonDAOImpl class extends HibernateTemplate so I think that it have to contain the operation of setting of the sessionFactory... 如您所见, PersonDAOImpl类扩展了HibernateTemplate,因此我认为它必须包含sessionFactory的设置操作...
The problem is that when I try to run this MainApp class I obtain the following exception: 问题是,当我尝试运行此MainApp类时,出现以下异常:
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:323)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:392)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)
at org.andrea.myexample.HibernateOnSpring.dao.PersonDAOImpl.addPerson(PersonDAOImpl.java:12)
at org.andrea.myexample.HibernateOnSpring.MainApp.main(MainApp.java:26)
Why I have this problem? 为什么我有这个问题? how can I solve it?
我该如何解决?
To be complete I also insert my pom.xml containing my dependencies list: 为了完整起见 ,我还插入了包含依赖项列表的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>org.andrea.myexample</groupId>
<artifactId>HibernateOnSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HibernateOnSpring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Dipendenze di Spring Framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency> <!-- Usata da Hibernate 4 per LocalSessionFactoryBean -->
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<!-- Dipendenze per AOP -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Dipendenze per Persistence Managment -->
<dependency> <!-- Apache BasicDataSource -->
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency> <!-- MySQL database driver -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
<dependency> <!-- Hibernate -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.9.Final</version>
</dependency>
</dependencies>
</project>
You should be using Springs Hibernate4 support 您应该使用Springs Hibernate4支持
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
More details here: http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/ 此处有更多详细信息: http : //blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/
Since HibernateTemplate
is no longer supported in Hibernate 4.x and my current project is riddled with the use of HibernateTemplate
I've opted to create and use my own custom HibernateTemplate
. 由于
HibernateTemplate
4.x不再支持HibernateTemplate,并且我当前的项目充满了HibernateTemplate
的使用,因此我选择创建和使用自己的自定义HibernateTemplate
。
Rather than extending HibernateDaoSupport
I just added a getHibernateTemplate()
method to my DAO. 我没有扩展
HibernateDaoSupport
而是向我的DAO添加了getHibernateTemplate()
方法。 In my custom HibernateTemplate
I then use the org.hibernate.SessionFactory
to perform the same operations. 然后,在我的自定义
HibernateTemplate
使用org.hibernate.SessionFactory
来执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.