我正在使用JPA和EJB创建Java EE应用程序。 @PersistenceContext(unitName =" airline")中的问题,当我对此行进行注释时,它会给出错误null_pointer_exception,并在代码在服务器上运行时收到以下错误。
Severe: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.airline.service.Test2EJBService/entityManager] in the scope of the module called [Test2]. Please verify your application.
我的班级:
AddPassenger2
@WebServlet(name = "AddPassenger2", urlPatterns = {"/AddPassenger2"})
public class AddPassenger2 extends HttpServlet {
@EJB
Test2EJBService bService;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AddPassenger</title>");
out.println("</head>");
out.println("<body>");
out.println("<h4> Hello Every body in Test2 :) <h4>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
Passenger passenger = new Passenger();
passenger.setFirstName("Loai");
passenger.setLastName("Amr");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1996);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DAY_OF_MONTH, 30);
Date dob = cal.getTime();
passenger.setDob(dob);
passenger.setGender(Gender.Male);
passenger.setFlightClass(FlightClass.Couch);
bService.addPassenger(passenger);
System.out.println("First name : " + passenger.getFirstName());
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我的EJB容器:
@Stateless
@LocalBean
public class Test2EJBService {
// @PersistenceUnit(unitName = "Test2PU")
@PersistenceContext(unitName = "airline")
private EntityManager entityManager;
public void addPassenger(Passenger passenger) {
// EntityManager createEntityManager;
// createEntityManager = Persistence.createEntityManagerFactory("Test2PU").createEntityManager();
entityManager.persist(passenger);
System.out.println("Done");
}
}
和persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Test2PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/airline</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
任何身体都可以帮助我在相同的错误中度过3天并且无法解决它。
以及我们与数据库的连接: enter image description here
答案 0 :(得分:0)
persistence-unit-name
文件中的persistence.xml
仍然显示“Test2PU”而不是“航空公司”。验证。另外,请确保在服务器上使用相同名称(即航空公司)正确配置了连接池和JDBC资源