【问题标题】:Why EntityManager have null value in my dao layer?为什么 EntityManager 在我的 dao 层中有空值?
【发布时间】:2016-09-01 17:05:00
【问题描述】:

我正在开发一个 Java EE 7 maven 项目,我正在使用 Wildfly 8.2,一切都很好,问题是当我使用托管 bean(支持 bean)中的 @PersistenceContext 创建一个实体管理器时,我与我的 jsf 一起使用容器创建一个实体管理器对象,它可以工作,但是当我尝试在我的 DAO 层中使用实体管理器时,它不起作用,它们保持为空值,我不知道为什么我的 dao 层中的代码可以帮助我? .

dao接口:

 public interface ICategoryDao {
      Category addCategory(Category category);
      void deleteCategory(Long codeCategory);
      Set<Category> getAllCategories();
      Category updateCategory(Category category);

    }

dao 实现:

@Named("categoryDao")
public class CategoryDao  implements ICategoryDao{

    private Logger log = Logger.getLogger(CategoryDao.class);
    @PersistenceContext(unitName="BooksStore")
    private EntityManager em ;
    @Override
    public Category addCategory(Category category) {
        if(em==null)
        {
            log.info("em is null ");
            return category;
        }
        em.getTransaction().begin();
        em.persist(category);
        em.getTransaction().commit();
        log.info("CategoryDao : Object persisted." );
        return category;
    }

    @Override
    public void deleteCategory(Long codeCategory) {
        // TODO Auto-generated method stub

    }

    @Override
    public Set<Category> getAllCategories() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Category updateCategory(Category category) {
        // TODO Auto-generated method stub
        return null;
    }

}

这是我的 beans.xml

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">

</beans>

我的 persistance.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="BooksStore" transaction-type="JTA">
    <jta-data-source>java:/bookstore</jta-data-source>
    <properties>
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
    </persistence-unit>
</persistence>

【问题讨论】:

  • 你有一个使用ICategoryDao的例子吗?
  • 谢谢它现在可以工作了,只是我添加了这两个 EJB 注释:“@Singleton”“@TransactionManagement(TransactionManagementType.CONTAINER)
  • @ErrabiAyoub,因为您自己找到了答案,您应该将其作为答案发布并接受。

标签: jpa jakarta-ee cdi ejb-3.0 wildfly-8


【解决方案1】:

你有persistence.xml 文件吗? 这是 persistence.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="School_Manager" transaction-type="RESOURCE_LOCAL">


      <class>YourEntityFQN</class>     


   <properties>

persistence.xml 文件上的属性名称在您的情况下必须是“BooksStore”。 在 class 标记中,您需要指定要用于此 persistence-unit 的所有实体。

【讨论】:

  • 是的,数据库是生成的
  • 您可以尝试将您的事务类型属性从 JTA 更改为 RESOURCE_LOCAL 吗?尝试删除 标签。告诉我它是否有效。
  • 是的,我认为如果我在 tomcat 上运行我的应用程序,您的解决方案将起作用,但我使用的是 JTA,数据源部署在 Wildfly 服务器上,并且所有表都是在数据库上创建的,这意味着jpa 配置没有问题我认为我的问题在于 CDI 注入
  • 是的...我认为您可以更改 beans.xml 文件并添加更常用的命名空间并配置 id="datasource" 的 Bean,这通常用于引用数据库带有主机、端口、用户名和密码。你可以在网上找到几个例子
  • 请显示带有CategoryDao调用站点的代码以及它的声明。如果 CategoryDao 是由容器创建的,您只会获得注入的 EntityManager。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 2018-11-13
  • 2013-10-18
  • 2018-03-12
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
相关资源
最近更新 更多