简体   繁体   中英

Unable to retrieve EntityManagerFactory for unitName null

I try to use JSPs by creating a simple Eclipse dynamic web project.

I made a basic JSP and a form that creates a parameterized request to a HttpServlet. This servlet should just use a entity bean to store the data into a database.

I used mapping via annotations.

This is the invoking part of my jsp:

<form action="persistencedemo" method="get">
    <input type="submit" name="submit" value='ok'/>
    <input type="hidden" name="firstName" value='<jsp:getProperty property="firstname" name="newCustomer"/>' />
    <input type="hidden" name="lastName"  value='<jsp:getProperty property="lastname" name="newCustomer"/>' />  
</form>

... this is the main section of my bean:

@Entity
@Table(name="CUSTOMERS")
public class CustomerBean implements Serializable {

@Id
@Column(name = "CUSTOMER_ID")
private Long id;

@Column(name = "FIRST_NAME")
private String firstname;

@Column(name = "LAST_NAME")
private String lastname;

public CustomerBean() {
    this.firstname = "";
    this.lastname = "";
}

    // Getters & Setters

and now - finally - this is my very simple servlet:

@WebServlet(name="PersistenceDemo", urlPatterns = {"/persistencedemo"})
public class PersistenceDemo extends HttpServlet {


    private static final long serialVersionUID = 1L;
    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    @Resource
    private UserTransaction userTransaction;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {


        EntityManager em = entityManagerFactory.createEntityManager();
        CustomerBean c1 = new CustomerBean();
        CustomerBean c2 = new CustomerBean();

        c1.setFirstname("My");
        c1.setLastname("Self");
        c1.setId(3L);

        c2.setId(4L);
        c2.setFirstname(req.getParameter("firstName"));
        c2.setLastname(req.getParameter("lastName"));

        try {
            userTransaction.begin();
            em.persist(c1);
            em.persist(c2);
            userTransaction.commit();
        }
        catch (Exception e) {
            e.printStackTrace();
        }


        resp.getWriter().print("Updated database!");

    }
}

[Update] My persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="hello-world" transaction-type="RESOURCE_LOCAL">
    <class>com.jpa.PersistenceDemo</class>  
        <properties>

            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/customerdb" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.user" value="customeruser" />
            <property name="javax.persistence.jdbc.password" value="customerpass" />
      </properties>
   </persistence-unit>
</persistence>      

Problem : I linked the mySQL connector and created the tables, but all the time when I am invoking the entityManagerFactory.createEntityManager() it crashes with the error message:

Unable to retrieve EntityManagerFactory for unitName null 

Well, aside from the fact that I should probably use another structure to separate model (bean) from view (jsp):

What is the problem here?

  1. Where is your persistence.xml file located?

  2. Maybe the project has incorrect structure? Is EJB in its own separate part? Try packing the parts of the project using CDI or into Enterprise Application .

Try creating a "New Enterprise Project" in whatever program in which you write Java and see what is automatically generated if you're unsure of what I'm talking about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM