无法创建JPA EntityManager;嵌套异常

我对Spring Boot有点陌生,当时我正在做一个小型基础项目。我做错了,但看不到哪里。 当我将代码作为Java应用程序运行时,出现以下错误:

Type Exception Report

Message Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Could not create JPA EntityManager; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Could not create JPA EntityManager; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)

我不知道问题出在哪里。 application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/student
spring.datasource.username=root
spring.datasource.password=

DAO实施:

package com.akyildiz.st.student.databaseaccesobject;

import java.util.List;
import javax.persistence.EntityManager;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.akyildiz.st.student.character.Student;


@Repository
public class StudentDAOImplementation implements StudentDatabaseAccessObject {
    @Autowired
    private EntityManager entityManager;

    @Override
    public List<Student> get() {
        Session currSession = entityManager.unwrap(Session.class);
        Query<Student> query = currSession.createQuery("from Student", Student.class);
        List<Student> list = query.getResultList();
        return list;
    }

    @Override
    public Student get(long id) {
        Session currSession = entityManager.unwrap(Session.class);
        Student st = currSession.get(Student.class, id);
        return st;
    }

    @Override
    public void save(Student student) {

        Session currSession = entityManager.unwrap(Session.class);
        currSession.saveOrUpdate(student);
    }

    @Override
    public void delete(long id) {
        Session currSession = entityManager.unwrap(Session.class);
        Student st = currSession.get(Student.class, id);
        currSession.delete(st);
    }
}
评论
  • uvero
    uvero 回复

    将以下行添加到配置中:

    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    

    当然,您需要jdbc驱动程序,因此请将其添加到您的依赖项中(例如,如果使用maven,则将其更改):

    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.43'