![](/img/trans.png)
[英]required a bean of type 'org.hibernate.SessionFactory' that could not be found
[英]A bean of type 'org.hibernate.SessionFactory' that could not be found
我是Java Web开发的新手,在使用Hibernate时遇到很多麻烦。 我在线上看了很多例子,关于如何做到这一点,到目前为止,我还没有运气。 我注意到他们在网上使用了一些模式,其中很多都像波纹管一样。
@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.
我对此感到非常困惑,并且在休眠网站上找不到如何执行此操作的好例子。 我使用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();
}
}
控制器
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;
}
}
application.properties
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
由于休眠已经存在了将近二十年,因此许多教程已经过时了。 如果使用最近几年发布的教程,您将拥有轻松得多的时间。
最初,只能使用从SessionFactory
获得的Sessions
以特定于休眠的方式访问休眠。 在2006年,Java Persistence API标准创建了一种通过从EntityManagers
获得的EntityManagerFactory
访问Java中对象关系映射器的通用方法。 它的设计在很大程度上受到了Hibernate团队的影响,随着2006年秋天Hibernate 3.2的发布,它成为访问hibernate的首选方式。
也就是说,您发现的教程已经过时了十多年。 要查看现在如何完成,请查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.