在JBoss / WildFly中注入EntityManager [英] Injecting EntityManager in JBoss/WildFly

查看:341
本文介绍了在JBoss / WildFly中注入EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JPA,EJB和JBoss / WildFly。



我需要将 EntityManager 注入到我的应用。我试图按照以下方式执行:

  @Stateless 
@LocalBean
public class ProductsService实现IProductsService {

@PersistenceContext(unitName =myUnit)
EntityManager entityManager;

// ....
}

而我在 .war META-INF 目录中有 persistence.xml c $ c> archive:

 <?xml version =1.0encoding =UTF-8?> 
< persistence xmlns =http://java.sun.com/xml/ns/persistenceversion =1.0>
< persistence-unit name =myUnit>
< jta-data-source> java:jboss / datasources / ExampleDS< / jta-data-source>
<属性>
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =hibernate.show_sqlvalue =true/>
< / properties>
< / persistence-unit>
< /余辉>

JNDI数据源( java:jboss / datasources / ExampleDS

当我将应用程序部署到WildFly时,出现以下错误:

>

  JBAS011440:在部署my-web-app.war中找不到名为myUnit的持久性单元。 

我做错了什么?

解决方案

persistence.xml 总是驻留在 {持久化根单元} / META-INF /目录中。在war文件中,您必须在 WEB-INF / classes / META-INF 中放置 persistence.xml

例如:

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

更多信息:持久性单元


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?

解决方案

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

这篇关于在JBoss / WildFly中注入EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆