没有名为connect的EntityManager的持久性提供程序

时间:2014-12-16 22:14:17

标签: java persistence project

我在尝试启动servlet时遇到了一些错误。

我的servlet代码:

package servlets;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/users")
public class usersView extends HttpServlet {

    private static final long serialVersionUID = 1L;


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String userzy = "";
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("connect");
            EntityManager em = emf.createEntityManager();
            userzy += "<table>";
            userzy += "<td class='main'>id</td>";
            userzy += "<td class='main'>login</td>";
            userzy += "<td class='main'>hasło</td>";
            userzy += "<td class='main'>grupa</td>";
            /*for(User c : persons)
            {
                userzy+="<tr><td>"+c.getIdekey()+"</td><td>"+c.getLogin()+"</td><td>"+c.getPassword()+"</td><td>"+c.getGroup()+"</td></tr>";
            }*/
            userzy += "</table>";
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();

            out.println("<html>");
            out.println("<meta http-equiv=\"content-type\" content=\"text/html;charset=UTF-8\" />");
            out.println("<head>");

            out.println("<title>Mój projekcik!</title>");
            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/global.css\" />");
            out.println("</head>");
            out.println("<body>");
            out.println("<div class='header'></div>");
            out.println("<div class='left_block'>");
            out.println("<div class='logo'></div>");
            out.println("<ul class='left_menu'>");
            out.println("<a href='index.jsp'><li>Strona główna</li></a>");
            out.println("<a href='events'><li>Eventy</li></a>");
            out.println("<a href='groups'><li>Grupy</li></a>");
            out.println("<a href='categories'><li>Kategorie</li></a>");
            out.println("</ul>");
            out.println("</div>");
            out.println("<div class='right_block'>");
            /*if(repo.getAll().size() <= 0) {
                out.println("<div class='i_error'>");
                out.println("There is no users in database!");
                out.println("</div>");
            }*/
            out.println("<div class='i_message'>");
            out.println(userzy);
            out.println("</div>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
            out.close();
            em.close();
            emf.close();
    }
}

我的persistence.xml代码

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

    <persistence-unit name="connect" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="Pariston"/>
            <property name="hibernate.connection.password" value="123321"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>
</persistence>

我的项目文件的结构视图(请告诉我这些directiores是否有错误) structure view

有人可以告诉我如何修复它吗?在此先感谢:)

1 个答案:

答案 0 :(得分:0)

将您的persistence.xml移至:

  

幅/ WEB-INF /

为什么资源和java文件夹下有META-INF?它应位于web下。 发布您的web.xml

并不错

其他链接: