STS Spring EntityManager 连接 500

STS Spring EntityManager connection 500

我正在将我的项目与 DB 连接

它是控制器的一部分

public @ResponseBody List<Account> getUserList() {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("mysql://kimdg3550.cafe24.com:3306/;user=kimdg3550;password=secret");     
    AccountDaoImpl.entityManager = emf.createEntityManager();
    return AccountDaoImpl.getAccount();
}

我收到这个错误

HTTP Status 500 - Request processing failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named mysql://kimdg3550.cafe24.com:3306/;user=kimdg3550;password=secret

type Exception report

message Request processing failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named mysql://kimdg3550.cafe24.com:3306/;user=kimdg3550;password=secret

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named mysql://kimdg3550.cafe24.com:3306/;user=kimdg3550;password=secret org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause

javax.persistence.PersistenceException: No Persistence provider for EntityManager named mysql://kimdg3550.cafe24.com:3306/;user=kimdg3550;password=secret javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85) javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) com.myongjoon.spring.HomeController.getUserList(HomeController.java:41) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:497) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note The full stack trace of the root cause is available in the Pivotal tc Runtime 3.1.2.RELEASE/8.0.26.B.RELEASE logs.

Pivotal tc Runtime 3.1.2.RELEASE/8.0.26.B.RELEASE

我认为我的数据库 URL 正确。

那么我应该怎么做才能避免这个错误?

如果您真的打算使用这种方法(我希望您不会),请在您的类路径中创建一个如下所示的 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value=""/>
            <property name="hibernate.connection.driver_class" value=""/>
            <property name="hibernate.connection.username" value=""/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

并使用持久性单元名称,在本例中为NewPersistenceUnit,作为createEntityManagerFactory的参数:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("NewPersistenceUnit");

在现实世界中,您应该在启动时创建一个 EntityManagerFactory,并在每次需要时使用该实例创建一个 EntityManager。使用您当前的方法,对 getUserList 的每个请求都会创建一个新的 EntityManagerFactory,这不是一个好主意。

如果您打算使用 Spring 框架,请使用 LocalContainerEntityManagerFactoryBean 工厂 bean 以创建 EntityManagerFactory.

的实例