gpt4 book ai didi

java - 无法找到类型为 'org.hibernate.SessionFactory' 的 bean

转载 作者:行者123 更新时间:2023-12-02 02:04:33 26 4
gpt4 key购买 nike

我是 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.

我对此感到非常困惑,并且我在 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();
}

}

Controller

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

最佳答案

由于 hibernate 已经存在了近二十年,许多教程都已经过时了。如果您使用过去几年发布的教程,您会轻松得多。

最初只能使用从 SessionFactory 获取的 Sessions 以特定于 hibernate 的方式访问 hibernate。 2006 年,Java Persistence API 标准创建了一种通过从 EntityManagerFactory 获取的 EntityManagers 访问 Java 中的对象关系映射器的通用方法。它的设计深受 Hibernate 团队的影响,并随着 2006 年秋季 Hibernate 3.2 的发布而成为访问 hibernate 的首选方式。

也就是说,您发现的教程已经过时了十多年。要了解现在是如何完成的,请查看

关于java - 无法找到类型为 'org.hibernate.SessionFactory' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51006149/

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