gpt4 book ai didi

java-ee-6 - 如何将@PersistenceContext 注入(inject) pojo 类

转载 作者:行者123 更新时间:2023-12-04 06:34:20 24 4
gpt4 key购买 nike

我正在尝试使用 @PersistenceContex 注释将 PersistenceContex 注入(inject) POJO,我读过我需要让 POJO 设法做到这一点。所以我将我的 POJO 类注入(inject)到 servlet 中(所以它现在作为依赖对象管理,对吗?)但是当 servlet 试图从注入(inject)的对象调用 metod 时,我得到错误:

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null



所以看起来 PersistenceContext 没有正确注入(inject) POJO,我应该怎么做才能让它工作?

我的 POJO 类如下所示:
public class FileEntityControlerImpl implements FileEntityInterface {

@PersistenceContext
EntityManager entityManager;

@Override
public void createFile(FileEntity fileEntity) {
...}

@Override
public FileEntity retriveFile(String fileName) {
...}

注入(inject)点:
@Inject
FileEntityInterface fileController;

如果我使用 SLSB 并使用 @EJB 注入(inject)它工作正常。

..::更新::..

堆栈跟踪:

WARNING: StandardWrapperValve[ResourcesServlet]: PWC1406: Servlet.service() for servlet ResourcesServlet threw exception java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName MambaPU at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:121) at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:162) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:554) at pl.zawi.mamba.core.integration.controllers.implementation.FileEntityControlerImpl.retriveFile(FileEntityControlerImpl.java:32) at pl.zawi.mamba.core.face.servlets.ResourcesServlet.doGet(ResourcesServlet.java:60) at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) at com.sun.grizzly.ContextTask.run(ContextTask.java:69) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) at java.lang.Thread.run(Thread.java:662)



持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="MambaPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/MambaDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<!-- <property name="javax.persistence.jdbc.password" value="root"/>-->
<!-- <property name="javax.persistence.jdbc.user" value="root"/>-->
<!-- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="eclipselink.ddl-generation" value="create-tables"/>-->
<!-- <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>-->
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>

..::UPDATE2::..

如果有人对我的项目感兴趣,我一直在使用 maven,所以它应该很容易构建和运行。(MySql 驱动器不包含在 pom-s 中,所以请记住)

Mamba.Core

最佳答案

仅供引用:

您根本不会在 Entity 类上使用 @PersistenceContext 注释。简单地在 POJO 中包含一个 Persistence Unit 将使它们得到管理(将 persistence.xml 和一个空 beans.xml 添加到 POJO 类的 JAR 的 META-INF 文件夹中。

@PersistenceContext 用于 session Bean,其目的是自动将 EntityManager 注入(inject) session bean。

关于java-ee-6 - 如何将@PersistenceContext 注入(inject) pojo 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5029571/

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