EJB @PersistenceContext EntityManager 抛出 NullPointerException [英] EJB @PersistenceContext EntityManager Throws NullPointerException
问题描述
我在使用 @PersistenceContext 注入 EntityManager 时遇到问题.我尝试使用 @PersistenceContext 在 EJB 类中注入 EntityManager,但我总是得到 NullPointerException.
I'm having a problem with injecting EntityManager by using @PersistenceContext. I try to inject EntityManager in EJB class with @PersistenceContext and I always get NullPointerException.
这是 EJB 类:
@Stateless
public class BookEJB {
public BookEJB(){
}
@PersistenceContext(unitName = "BookWebStorePU")
private EntityManager em;
public Book createBook(Book book) throws Exception {
System.out.println("EM: " + em);
em.persist(book);
return book;
}
public Book findBookByIsbn10(String isbn10){
Book book = new Book();
em.find(Book.class, isbn10);
return book;
}
//Other methods here
}
这是 Persistence.xml
Here's Persistence.xml
<?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="BookWebStorePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/BookWebStoreDB"/>
<property name="javax.persistence.jdbc.user" value="bookwebstoreadmin"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<!-- Let EclipseLink create database schemas automatically -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
</properties>
这是我的测试文件:
public class BookDaoTests {
private BookEJB bookDao;
private Book newBook;
@Before
public void init() {
newBook = new Book();
newBook.setName("Flying Cow");
newBook.setDescription("Super cool story about flying cow");
newBook.setAuthor("Me");
newBook.setIsbn10("0123456789");
newBook.setIllustrations(true);
newBook.setPublishYear(2013);
newBook.setNumberOfPages(1567);
newBook.setQuantity(58);
bookDao = new BookEJB();
}
@Test
public void createBook() throws Exception{
bookDao.createBook(newBook);
Assert.assertEquals("Book was created!", bookDao.findBookByIsbn10("0123456789"), newBook);
}
}
因此,当我运行该测试文件时,出现以下错误:
So when I run that test file I get following error:
Testcase: createBook(com.mysite.bookstore.tests.BookDaoTests): Caused an ERROR
null
java.lang.NullPointerException
at com.mysite.bookwebstore.beans.BookEJB.createBook(BookEJB.java:27)
at com.mysite.bookstore.tests.BookDaoTests.createBook(BookDaoTests.java:46)
EM: null
我使用以下技术:
- 玻璃鱼 4
- JavaEE 7
- JSF
- EclipseLink 2.1
- Java 数据库
我希望我们能找到解决这个问题的办法.我现在已经解决了这个问题 3 天,并从 Google 和 Stackoverflow 搜索并测试了解决方案,但没有一个解决方案有帮助/有效.为了确保 EntityManager 真的为空,我调试了测试文件并看到它确实给出了空值.那么我该如何解决这个问题呢?
I hope we can find some solution for this problem. I have been tackling now 3 days of this problem and searched and tested solutions from Google and from Stackoverflow but none of the solutions helped/worked. To make sure that the EntityManager was really null, I debugged test file and saw that indeed it gives null. So how can I solve this problem?
推荐答案
EntityManager 实例,在 EJB 部署到 Container 时注入.如果您查看企业的生命周期bean,你会清楚地看到什么时候发生依赖注入.当容器看到@Persistencecontext 注解时,它会注入一个容器管理的 EntityManager.
The EntityManager instance, is injected when the EJB is deployed in the Container. If you take a look at the lifecycle of enterprise bean, you will see clearly when dependency injection occurs. When the Container sees the @Persistencecontext annotation it will inject a container-managed EntityManager.
问题是这个测试中执行的代码不是由Container管理的,因此没有人注入必要的依赖项.
The problem is that the code executed in this test is not managed by the Container, therefore, no one inject the necessary dependencies.
bookDao = new BookEJB();
当你运行测试时,BookEJB 类只是一个简单的 POJO,@Stateless 和 @PersistenceContext 注释被简单地忽略了.
When you run the test, the BookEJB class is just a simple POJO, the @Stateless and @PersistenceContext annotations are simply ignored.
为了测试 EJB,您有多种选择,请查看此链接一>.
You have several alternatives in order to test your EJB, take a look at this link.
这篇关于EJB @PersistenceContext EntityManager 抛出 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!