为什么EntityManager在我的dao层中有空值?

时间:2016-09-01 17:05:01

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

我正在开发一个Java EE 7 maven项目我正在使用wildfly 8.2一切都很糟糕问题是当我使用@tersistenceContext创建一个实体管理器在托管bean(支持bean)中使用我的jsf,contanier创建一个权限管理器对象,它的工作,但当我尝试在我的DAO层中使用实体maanger时,它不起作用,em逗留有一个空值,我不知道为什么我的dao层中的代码可以帮助我?

dao界面:

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

    }

dao impl:

@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>

1 个答案:

答案 0 :(得分:-2)

你有一个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”。 在类标记中,您需要指定要用于此持久性单元的所有实体。