gpt4 book ai didi

java - 使用 HibernateTemplate 的问题 : java. lang.NoSuchMethodError : org. hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;

转载 作者:搜寻专家 更新时间:2023-10-31 20:04:02 25 4
gpt4 key购买 nike

我是 Spring 世界的新手,我疯狂地尝试使用 HibernateTemplate 抽象支持类将 Hibernate 集成到 Spring 应用程序中

我有以下类要保留在数据库表上:

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;
}
}

在它旁边,我创建了一个名为 PersonDAO 的接口(interface),我只在其中定义了我的 CRUD 方法。所以我通过一个名为 PersonDAOImpl 的类实现了这个接口(interface),该类还扩展了 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

}

}

(目前我只尝试实现 addPerson() 方法)

然后我创建了一个主类来测试向数据库表中插入一个新对象的操作:

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");
}

}

如您所见PersonDAOImpl扩展了HibernateTemplate所以我认为它必须包含设置sessionFactory的操作...

问题是,当我尝试运行这个 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)

为什么我有这个问题?我该如何解决?

为了完整,我还插入了包含我的依赖项列表的 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>

最佳答案

你应该使用 Springs Hibernate4 支持

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

更多细节在这里: http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/

关于java - 使用 HibernateTemplate 的问题 : java. lang.NoSuchMethodError : org. hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15050453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com