java EntityManager 注入 - NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EntityManager injection - NullPointerException
提问by John Manak
In my Spring+JPA/Hibernate+Wicket app, I have a QueryBuilder bean that I want to use in one of my DAOs which generates a typed query with the help of Criteria API:
在我的 Spring+JPA/Hibernate+Wicket 应用程序中,我有一个 QueryBuilder bean,我想在我的一个 DAO 中使用它,它在 Criteria API 的帮助下生成类型化查询:
@Service(value="inboxQueryBuilder")
public class InboxQueryBuilder {
@PersistenceContext
EntityManager em;
CriteriaBuilder cb;
public InboxQueryBuilder() {
cb = em.getCriteriaBuilder();
}
public TypedQuery<App> getQueryForApps(AppSearchObject aso) {
...
}
...
}
However, when I run the app, I get a null pointer exception for line:
但是,当我运行应用程序时,我得到了一个空指针异常:
cb = em.getCriteriaBuilder();
i.e. the EntityManager doesn't get injected. Do you know why?
即 EntityManager 没有被注入。你知道为什么吗?
Also, is this use correct and thread-safe or should I instantiate my InboxQueryBuilder for each query? In that case, should I also inject the EntityManager or should I just pass it as a constructor parameter (the InboxQueryBuilder would get instantiated for each query in the DAO which has an injected instance of EntityManager)?
此外,这种使用是否正确且线程安全,还是应该为每个查询实例化 InboxQueryBuilder?在这种情况下,我还应该注入 EntityManager 还是应该将它作为构造函数参数传递(InboxQueryBuilder 将针对 DAO 中具有注入的 EntityManager 实例的每个查询进行实例化)?
回答by Christian Kuetbach
You can't access the EntityManager within the constructor. Take a look at the @PostConstruct-Annotation
您无法在构造函数中访问 EntityManager。看看@PostConstruct-Annotation
@Service(value="inboxQueryBuilder")
public class InboxQueryBuilder {
@PersistenceContext
EntityManager em;
CriteriaBuilder cb;
public InboxQueryBuilder() {
// em= null
}
@PostConstruct
public void toSomething(){
// em set by Container
cb = em.getCriteriaBuilder();
}
public TypedQuery<App> getQueryForApps(AppSearchObject aso) {
...
}
...
}
EDIT:After reading your post again, I start to became unsure, if I'm right. I know the Java EE-Dependency-Injection within a JBoss works as I described, but I'm not sure about spring-IOC.
编辑:再次阅读您的帖子后,我开始不确定,如果我是对的。我知道 JBoss 中的 Java EE-Dependency-Injection 按我的描述工作,但我不确定 spring-IOC。
回答by nanda
Do you have this bean somewhere in your application context?
您的应用程序上下文中是否有这个 bean?
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPersistenceUnit"/>
</bean>
回答by Sean Patrick Floyd
Spring uses the Java Beans mechanism, so I am pretty sure this is insufficient:
Spring 使用 Java Beans 机制,所以我很确定这是不够的:
@PersistenceContext
EntityManager em;
Here's the standard way:
这是标准方法:
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(final EntityManager entityManager){
this.entityManager = entityManager;
}