2

I'm working on a Java EE 7 maven project I'm using wildfly 8.2 everything is okey the problem is when I create an entity manager using @PersistenceContext inside managedbeans (backing beans) that I use with my jsf the contanier create a entit manager object and it's work but when I try to use the entity maanger inside my DAO Layer it's not work the em stay have a null value and I don't know why this my code in my dao layer can someone helpe me ? .

dao interface :

 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;
    }

}

this is my 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>

My persistance.xml file :

<?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>
e2rabi
  • 4,728
  • 9
  • 42
  • 69

1 Answers1

-2

Do you have a persistence.xml file ? This is an example for persistence.xml file :

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

attribute name on persistence.xml file must be "BooksStore" in your case . In class tag you need to specify all entity that you want use for this persistence-unit .

pioardi
  • 181
  • 2
  • 7
  • Yes The database was generated my problem is with the both annotation Inject and PersistenceContext doesn't work when I want to create my entity manager so my em stay have the null value – e2rabi Sep 01 '16 at 17:44
  • 1
    Can you try yo change your transaction-type attribute from JTA to RESOURCE_LOCAL please ?Try to remove tag . Say me if it works. – pioardi Sep 01 '16 at 18:13
  • Yes I think that your solution will work if I run my app on tomcat, but I'm using JTA the datasource was deployed on wildfly server and all the tables was created on the database that means that is not poblem with jpa configuration I think that my problem is with CDI injection – e2rabi Sep 01 '16 at 18:44
  • Yes...I m thi king that you Can change your beans.xml file And add a more usual namespaces And configure a Bean with id="datasource" , this is normally use to refer a db with Host, Port, username And password. You Can Found several example online – pioardi Sep 01 '16 at 19:08
  • This is an example of configuration, but with Spring framework : Probably you need a similar configuration . – pioardi Sep 01 '16 at 19:20
  • https://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html There are several example in this link . Good lucky! – pioardi Sep 01 '16 at 19:24
  • 1
    Please show the code that with the `CategoryDao` call site and also it's declaration. You will only get an EntityManager injected if the CategoryDao is created by the container. – Steve C Sep 02 '16 at 03:24
  • You don't want to use a `RESOURCE_LOCAL` transaction type in managed containers. – James R. Perkins Sep 02 '16 at 23:48
  • Thank you It's work now, just I add these two EJB annotations : "@Singleton" "@TransactionManagement(TransactionManagementType.CONTAINER) – e2rabi Sep 03 '16 at 12:59