javaapachehibernatejakarta-eedeltaspike

Cannot find EntityManager Apache Deltaspike


I try to print the number of rows of a query included in a Repository. Here is the Repository :

@Repository
public interface TagRepository extends EntityRepository<Tag, Long> {

    /**
    * @param //
    * @return all Tag matched
    */
   @Query("SELECT * FROM Tag") //TESTED
   List<Tag> findByDefault(); 
}

Here is my Controller :

@Named
@ViewScoped
public class ControllerTest implements Serializable {

/**
 * 
 */
   private static final long serialVersionUID = 1L;
   @Inject
   private TagRepository tagRepository;

   public ControllerTest()
   {
       super();
   }

   public Integer compte()
   {
       return tagRepository.findByDefault().size();
   }

   public String essai()
   {
       String message = "Hello World !";
       return message;
   }
}

Here is my *.xhtml file :

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ds="http://deltaspike.apache.org/jsf"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich" template="/layout/template.xhtml" 
xmlns:a4j="http://richfaces.org/a4j">

<ui:define name="body">
    <rich:collapsiblePanel switchType="client" opened="false" label="Updates version 2.2.7">
            <h:outputText 
                value="#{controllerTest.compte()}">         
            </h:outputText>
            <h:outputText 
                value="#{controllerTest.essai()}">          
            </h:outputText>
            <h:outputText 
                value="Hello world !">          
            </h:outputText>
    </rich:collapsiblePanel>
</ui:define>

The layout runs perfectly with the method essai() but when I use the method compte() :

javax.servlet.ServletException: Exception calling Repository: [Repository=class repositories.TagRepository$$DSPartialBeanProxy,method=findByDefault],exception=class java.lang.IllegalStateException,message=Could not find EntityManager with default qualifier.

I though that Apache Deltaspike was able to manage beans by is own with annotations.

The question is : Where comes from this issue ? Is it my Repository ? Did I miss something ? I have still googled this one but nothing for Apache Deltaspike.

I use Wildfly 10.0.0, Hibernate 5.4.1, Eclipse Neon, RichFaces 4.X, JSF 2.X, Apache Deltaspike 1.7.2.

Thank you in advance for your answers.


Solution

  • DeltaSpike requires an EntityManager exposed via a CDI producer. For example:

    public class EntityManagerProducer {
    
        @PersistenceUnit
        private EntityManagerFactory emf;
    
        @Produces 
        public EntityManager create() {
            return emf.createEntityManager();
        }
    
        public void close(@Disposes EntityManager em) {
            if (em.isOpen()) {
                em.close();
            }
        }
    }
    

    You could find more documentation here: https://deltaspike.apache.org/documentation/data.html