找不到类型为'org.hibernate.SessionFactory'的bean

时间:2018-06-24 00:51:57

标签: java spring hibernate spring-mvc spring-boot

我是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/引导我的应用程序。贝娄是我的代码。

ENTITY

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

1 个答案:

答案 0 :(得分:2)

由于冬眠已经存在了将近二十年,所以许多教程已经过时了。如果使用最近几年发布的教程,您将拥有轻松得多的时间。

使用从TextAnnotation$class获得的TextAnnotation.class,只能以特定于休眠的方式访问休眠。 2006年,Java Persistence API标准创建了一种通过从Sessions获得的SessionFactory访问Java中对象关系映射器的通用方法。它的设计在很大程度上受到了Hibernate团队的影响,随着2006年秋季Hibernate 3.2的发布,它成为访问hibernate的首选方式。

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

相关问题