Webapp找不到持久性单元。范围内没有名为“X”的持久性单元

时间:2013-05-30 14:21:16

标签: maven jpa ejb jpa-2.0 persistence.xml

我正在尝试使用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();
}

}

这是persistence.xml文件,位于src / main / java - &gt;下面。日食建成的META-INF。

<?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中创建classes文件夹。这有效!非常感谢!

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

enter image description here

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

1 个答案:

答案 0 :(得分: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">