0

我尝试打印存储库中包含的查询的行数。这是存储库:

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

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

这是我的控制器:

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

这是我的 *.xhtml 文件:

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

布局与该方法完美运行,essai()但是当我使用该方法时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.

我认为 Apache Deltaspike 能够通过注释自己管理 bean。

问题是:这个问题从何而来?它是我的存储库吗?我错过了什么 ?我仍然用谷歌搜索了这个,但没有找到 Apache Deltaspike。

我使用 Wildfly 10.0.0、Hibernate 5.4.1、Eclipse Neon、RichFaces 4.X、JSF 2.X、Apache Deltaspike 1.7.2。

预先感谢您的回答。

4

1 回答 1

3

DeltaSpike 需要通过 CDI 生产者公开的 EntityManager。例如:

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

您可以在此处找到更多文档: https ://deltaspike.apache.org/documentation/data.html

于 2017-06-16T20:51:02.960 回答