Java 类型为'的bean;org.hibernate.SessionFactory';那是找不到的
我是Java web开发新手,在使用Hibernate时遇到了很多问题。我在网上看了很多关于如何做到这一点的例子,到目前为止,我没有任何运气让它发挥作用。我注意到他们在网上使用的一些模式,很多都像下面这样Java 类型为'的bean;org.hibernate.SessionFactory';那是找不到的,java,spring,hibernate,spring-mvc,spring-boot,Java,Spring,Hibernate,Spring Mvc,Spring Boot,我是Java web开发新手,在使用Hibernate时遇到了很多问题。我在网上看了很多关于如何做到这一点的例子,到目前为止,我没有任何运气让它发挥作用。我注意到他们在网上使用的一些模式,很多都像下面这样 @Autowired private SessionFactory sessionFactory; Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); // do somethi
@Autowired
private SessionFactory sessionFactory;
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
// do something with session
session.getTransaction().commit();
然而,每当我在我的项目中尝试这样做时,我都会遇到一个错误,即
Field sessionFactory in com.bT.practice.WebMySQLAspects.dao.StudentDAOImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.
我真的很困惑,在hibernate网站上找不到一个好的例子来说明如何做到这一点。我使用http://start.spring.io/
启动我的应用程序。下面是我的密码
实体
package com.bT.practice.WebMySQLAspects.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
public Student() {
}
public Student(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
DAO实施
package com.bT.practice.WebMySQLAspects.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.bT.practice.WebMySQLAspects.entity.Student;
@Repository
public class StudentDAOImpl implements StudentDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Student> getStudents() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
List<Student> students = session.createQuery("from Student order by lastName").list();
session.getTransaction().commit();
return students;
}
}
package com.bT.practice.WebMySQLAspects.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bT.practice.WebMySQLAspects.dao.StudentDAO;
import com.bT.practice.WebMySQLAspects.entity.Student;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDAO studentDAO;
@Override
@Transactional
public List<Student> getStudents() {
return studentDAO.getStudents();
}
}
由于hibernate已经存在了近二十年,许多教程已经过时得可怕。如果您使用过去几年发布的教程,您将有一个轻松得多的时间 最初,只能使用从
SessionFactory
获取的Sessions
以特定于hibernate的方式访问hibernate。2006年,Java持久化API标准创建了一种通过EntityManager
从EntityManager工厂获得的访问Java中对象关系映射器的通用方法。它的设计深受Hibernate团队的影响,随着2006年秋季Hibernate 3.2的发布,它成为访问Hibernate的首选方式
也就是说,您发现的教程已经过时十多年了。要了解它现在是如何完成的,请查看
- Spring团队的教程
- 和
由于hibernate已经存在了近二十年,许多教程已经过时得可怕。如果您使用过去几年发布的教程,您将有一个轻松得多的时间
最初,只能使用从
SessionFactory
获取的Sessions
以特定于hibernate的方式访问hibernate。2006年,Java持久化API标准创建了一种通过EntityManager
从EntityManager工厂获得的访问Java中对象关系映射器的通用方法。它的设计深受Hibernate团队的影响,随着2006年秋季Hibernate 3.2的发布,它成为访问Hibernate的首选方式
也就是说,您发现的教程已经过时十多年了。要了解它现在是如何完成的,请查看
- Spring团队的教程
- 和
您正在使用spring,并且您有一个带有@Transactional
注释的服务,您不需要启动事务并提交它。。。春天已经为你做了。此外,在简单的sql查询中不需要事务。首先搜索什么是数据库事务,然后搜索spring如何处理数据库事务。。。先了解事情,然后实施。哦,太好了,谢谢你。但它并没有解决我的问题。@dg2903您在bean中为SessionFactory创建了所需的条目了吗xml@AmanChhabra我没有任何bean.xml,如果我理解正确,如果我使用类似ComponentScan和Component的注释,我就不需要bean.xml。@AmanChhabra:这个问题用Spring Boot标记。Spring Boot自动创建必要的bean,不需要编写bean定义,更不用说XML了。您使用的是Spring,并且您有一个带有@Transactional
注释的服务,您不需要启动事务并提交它。。。春天已经为你做了。此外,在简单的sql查询中不需要事务。首先搜索什么是数据库事务,然后搜索spring如何处理数据库事务。。。先了解事情,然后实施。哦,太好了,谢谢你。但它并没有解决我的问题。@dg2903您在bean中为SessionFactory创建了所需的条目了吗xml@AmanChhabra我没有任何bean.xml,如果我理解正确,如果我使用类似ComponentScan和Component的注释,我就不需要bean.xml。@AmanChhabra:这个问题用Spring Boot标记。SpringBoot自动创建必要的bean,不需要编写bean定义,更不用说XML了。
package com.bT.practice.WebMySQLAspects.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.bT.practice.WebMySQLAspects.entity.Student;
import com.bT.practice.WebMySQLAspects.service.StudentService;
@RestController
@RequestMapping("/api")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students/show")
public List<Student> getStudents() {
List<Student> students = studentService.getStudents();
return students;
}
}
spring.datasource.driverClssName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false
spring.datasource.username=username
spring.datasource.password=password