gpt4 book ai didi

java - @PersistenceContext EntityManager 始终为 null

转载 作者:行者123 更新时间:2023-12-02 10:03:21 29 4
gpt4 key购买 nike

我正在使用 Wildfly 创建一个简单的 CRUD 应用程序来管理图书(查找和保留)

但是 EntityManager 总是返回 null。

我尝试了 Persistence.createEntityManagerFactory("PU") 方法,它工作正常。

我需要知道为什么我的代码无法工作。缺少什么或有什么问题?

谢谢

Personel level in Jee = beginner

Wildfly version 14

IDE Intellij

Java 7

JEE 7

ORM EclipseLink

Book.java:

package Model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Book {

@Id
private Long id;
private String title;
private String description;
private Float unitCost;
private String isbn;

public Book(Long id) {
this.id = id;
}

public Book() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Float getUnitCost() {
return unitCost;
}

public void setUnitCost(Float unitCost) {
this.unitCost = unitCost;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}
}

BookService.java

package Service;

import Model.Book;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.Transactional;


@Transactional
public class BookService {

@PersistenceContext(unitName = "applicationCDBookStorePU", type = PersistenceContextType.EXTENDED)
private EntityManager em;

public void persist(Book book) {
em.persist(book);
}

public Book find(long id) {
return em.find(Book.class, id);
}
}

持久性.xml

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

<persistence-unit name="applicationCDBookStorePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.jdbc.url" value="jdbc:postgresql://localhost/book"/>
<property name="eclipselink.jdbc.driver" value="org.postgresql.Driver"/>
<property name="eclipselink.jdbc.user" value="postgres"/>
<property name="eclipselink.jdbc.password" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

测试主线

package Service;

public class Test {
public void displayBook(long id) {
BookService bookService = new BookService();
System.out.println(bookService.find(id));
}
}

index.jsp

<%@ page import="Service.Test" %><%--
Created by IntelliJ IDEA.
Date: 02/04/2019
Time: 15:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
Test test = new Test();
test.displayBook(1);
%>
< /body>
</html>

堆栈跟踪:

org.apache.jasper.JasperException: java.lang.NullPointerException
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:473)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.jsp.JspFileHandler.handleRequest(JspFileHandler.java:32)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at Service.BookService.find(BookService.java:22)
at Service.Test.displayBook(Test.java:6)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:102)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
... 46 more

代码结构: image here

最佳答案

为了使用由 @PersistenceContext 注入(inject)的 EntityManager,它需要由您的容器 (Widlfly) 进行管理。

因此,您必须让容器在 @Inject 注解的帮助下负责创建这些类。

  • 为了使事情变得更简单,而不是使用 JSP 文件,请创建一个 JSF (index.xhtml),如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">


<h:body>
Message is: #{test.displayBook(1)}
</h:body>
</html>

  • 您的 Test 类需要使用 @Named("test") 进行注释才能从 jsf 访问:

重要提示:确保使用正确的导入。

package Service;

import javax.inject.Inject;
import javax.inject.Named;

@Named("test")
public class Test {

@Inject
private BookService bookService;

public String displayBook(long id) {
bookService.find(id);
return "Done!";
}
}

  • 您的 BookingService 可能如下所示:
package Service;


import Model.Book;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.Transactional;

@Transactional
public class BookService {

@PersistenceContext(unitName = "applicationCDBookStorePU", type = PersistenceContextType.EXTENDED)
private EntityManager em;

public void persist(Book book) {
em.persist(book);
}

public Book find(long id) {
System.out.println("EntityManager: " + em.toString());
return em.find(Book.class, id);
}

}
  • 保持您的图书模型不变。

  • 重新排列文件夹,如以下屏幕截图所示:

Project tree structure

  • 创建一个 beans.xml,如图所示,包含以下内容,这允许 CDI(JavaEE 依赖注入(inject)器)扫描类。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

部署您的应用程序,转到 http://localhost:PORT/CONTEXT/index.html

如果您在 Wildfly 日志中看到类似以下的条目:

INFO  [stdout] (default task-2) EntityManager: org.jboss.as.jpa.container.TransactionScopedEntityManager@54e9b6cd

EntityManager 已正确注入(inject)。

PS:遵循 Java 命名约定,包必须小写,以避免与类或接口(interface)的名称冲突 https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

PS2: maven 项目的标准目录布局,更多信息:https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

关于java - @PersistenceContext EntityManager 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477230/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com