【问题标题】:Cannot find EntityManager Apache Deltaspike找不到 EntityManager Apache Deltaspike
【发布时间】:2017-06-16 08:46:07
【问题描述】:

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

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

提前感谢您的回答。

【问题讨论】:

    标签: java apache hibernate jakarta-ee deltaspike


    【解决方案1】:

    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

    【讨论】:

    • 最后我不需要 EntityManager。这是我的依赖项之间的冲突。范围自动提供 Bean。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多