4

我正在尝试使用 EJB 3.1、JPA 2.0、M2E-WTP 和 JSF 2.1 技术在我的 weblogic 12c 服务器上运行我的 webapp,并继续收到以下错误:

javax.ejb.EJBException: EJB
Exception: : java.lang.IllegalArgumentException: No persistence unit named 'X'
is available in scope Webapp. Available persistence units: [] at
weblogic.persistence.ModulePersistenceUnitRegistry
.getPersistenceUnit(ModulePersistenceUnitRegistry.java:130) at
weblogic.persistence.BasePersistenceContextProxyImpl
.<init>(BasePersistenceContextProxyImpl.java:40) at
weblogic.persistence.TransactionalEntityManagerProxyImpl
.<init>(TransactionalEntityManagerProxyImpl.java:31) at
weblogic.persistence.EntityManagerInvocationHandlerFactory.
createTransactionalEntityManagerInvocationHandler
(EntityManagerInvocationHandlerFactory.java:20) at 
weblogic.persistence.PersistenceManagerObjectFactory
.createPersistenceContextProxy(PersistenceManagerObjectFactory.java:66) at 
weblogic.persistence.PersistenceManagerObjectFactory
.getObjectInstance(PersistenceManagerObjectFactory.java:31) at 
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:251) at
weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:406) at
weblogic.j2eeclient.java.ClientReadOnlyContextWrapper.lookup

这是触发异常的 EJBBean

@Stateless
public class QuoteSessionEJB implements QuoteSessionEJBLocal {

/**
 * Default constructor. 
 */
public QuoteSessionEJB() {
    // TODO Auto-generated constructor stub
}

@PersistenceContext(unitName="X")
private EntityManager em;
/**
* Returns a list of Quote objects in the database
* @return List<Customer>
*/
public List<Quote> getQuote() {
Query query = em.createNamedQuery("findQuotes");
return query.getResultList();
}

}

这是位于 src/main/java --> META-INF 下的 persistence.xml 文件,它是 eclipse 构建的。

<?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="X" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>localJTA</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.target-server" value="WebLogic"/>
        <property name="eclipselink.logging.level" value="FINEST"/>          
    </properties>
</persistence-unit>
</persistence>

我花了几个小时在网上搜索答案,但无济于事。我不明白为什么我的 persistence.xml 中的持久性单元 X 无法定位...

我希望有人遇到过同样的问题或知道如何解决它。任何帮助是极大的赞赏!

更新:根据下面的 better_use_mkstemp 评论,我将包含我的 persistence.xml 的 META-INF 文件夹从 src/main/java 移动到 WEB-INF/classes。我必须在 Eclipse 中创建类文件夹。这行得通!非常感激!

后续问题:进行更多研究后,我发现将 META-INF 文件夹放入 src/main/resources 也可以解决问题,但事实并非如此。我不知道这是否是 Maven 的 M2E-WTP eclipse 插件的问题,或者我只是在这里误解了一些东西。下面是文件夹结构的屏幕截图。

在此处输入图像描述

通过浏览 maven install 命令创建的 war 文件,我可以看到 META-INF 文件夹已正确放置在 WEB-INF/classes 文件夹中。但是在 Eclipse 中简单地发布到服务器是行不通的。除非我在 WEB-INF 下手动创建类文件夹并在其中手动删除/移动 META-INF 文件夹,否则我无法让它在 IDE 中工作。此外,在两个地方拥有 META-INF 文件夹的副本作为解决方案似乎没有意义。

4

1 回答 1

0

尝试在您的 persistence.xml 中替换:

<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 version="1.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_1_0.xsd">
于 2017-03-10T19:42:31.150 回答