STS Spring EntityManager连接500

时间:2016-02-25 15:07:54

标签: spring spring-mvc spring-data spring-tool-suite

我将我的项目与DB连接

它是Controller

的一部分
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状态500 - 请求处理失败;嵌套异常是javax.persistence.PersistenceException:没有名为mysql的EntityManager的持久性提供程序://kimdg3550.cafe24.com:3306 /; user = kimdg3550; password = secret

     

输入例外报告

     

消息请求处理失败;嵌套异常是javax.persistence.PersistenceException:没有名为mysql的EntityManager的持久性提供程序://kimdg3550.cafe24.com:3306 /; user = kimdg3550; password = secret

     

说明服务器遇到内部错误,导致无法完成此请求。

     

例外

     

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是javax.persistence.PersistenceException:没有名为mysql的EntityManager的持久性提供程序://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)   根本原因

     

javax.persistence.PersistenceException:没有名为mysql的EntityManager的持久性提供程序://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(原生方法)   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)   注意Pivotal tc Runtime 3.1.2.RELEASE / 8.0.26.B.RELEASE日志中提供了根本原因的完整堆栈跟踪。

     

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

我认为我的数据库网址正确无误。

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

1 个答案:

答案 0 :(得分:1)

如果您真的想要使用这种方法(我希望您不是这样),请在类路径中创建一个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 Framework,请使用LocalContainerEntityManagerFactoryBean工厂bean来创建EntityManagerFactory的实例。