-2

I'm connecting my project with DB

It is part of 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();
}

I get this error

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

I think my DB URL correct.

so what should I do to avoid this error?

김명준
  • 353
  • 3
  • 5
  • 19

1 Answers1

2

If you're seriously going to use this approach (which i'm hoping you're not), create a persistence.xml file in your classpath that looks like following:

<?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>

And use the Persistence Unit Name, in this case NewPersistenceUnit, as the parameter of createEntityManagerFactory:

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

In a real world, you should have one EntityManagerFactory created at startup time and use that instance to create an EntityManager, every time you need one. With your current approach, every request to getUserList would create a fresh EntityManagerFactory and that's not a good idea.

If you're planning to use Spring Framework, use LocalContainerEntityManagerFactoryBean factory bean in order to create an instance of EntityManagerFactory.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151