Injecting EntityManager in JBoss/WildFly

纵然是瞬间 提交于 2019-12-22 08:38:48

问题


I am learning JPA, EJB and JBoss/WildFly.

I need to inject an EntityManager into my application. I am trying to do it in the following way:

@Stateless
@LocalBean
public class ProductsService implements IProductsService {

    @PersistenceContext(unitName = "myUnit")
    EntityManager entityManager;

//....
}

And I have persistence.xml file in the META-INF directory in my .war archive:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="myUnit">
        <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> 
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

The JNDI data source (java:jboss/datasources/ExampleDS) is the default data source that is provided in a clean WildFly installation.

When I deploy my application to WildFly I get the following error:

JBAS011440: Can't find a persistence unit named myUnit in deployment "my-web-app.war".

What am I doing wrong?


回答1:


persistence.xml always resides in {root-of-persistence-unit}/META-INF/ directory. In a war file you must place the persistence.xml in WEB-INF/classes/META-INF.

Eg:

WEB-INF/classes/META-INF/persistence.xml
WEB-INF/web.xml

See more: Persistence Units



来源:https://stackoverflow.com/questions/28549026/injecting-entitymanager-in-jboss-wildfly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!