EJB @PersistenceContext EntityManager抛出NullPointerException [英] EJB @PersistenceContext EntityManager Throws NullPointerException
问题描述
我使用@PersistenceContext注入EntityManager有问题。我尝试用@PersistenceContext在EJB类中注入EntityManager,并且我总是得到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);
返回书;
}
public Book findBookByIsbn10(String isbn10){
Book book = new Book();
em.find(Book.class,isbn10);
返回书;
}
//其他方法
}
这里是持久性.xml
<?xml version =1.0encoding =UTF-8?>
< persistence version =2.1xmlns =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 =BookWebStorePUtransaction-type =RESOURCE_LOCAL>
< provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider>
< exclude-unlisted-classes> false< / exclude-unlisted-classes>
< validation-mode> NONE< / validation-mode>
<属性>
< property name =javax.persistence.schema-generation.database.actionvalue =create/>
< property name =javax.persistence.jdbc.drivervalue =org.apache.derby.jdbc.EmbeddedDriver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:derby:// localhost:1527 / BookWebStoreDB/>
< property name =javax.persistence.jdbc.uservalue =bookwebstoreadmin/>
< property name =javax.persistence.jdbc.passwordvalue =password/>
<! - 让EclipseLink自动创建数据库模式 - >
< property name =eclipselink.ddl-generationvalue =drop-and-create-tables/>
< property name =eclipselink.ddl-generation.output-modevalue =database/>
< / properties>
这是我的测试文件:
public class BookDaoTests {
private BookEJB bookDao;
私人书籍新书;
@Before
public void init(){
newBook = new Book();
newBook.setName(Flying Cow);
newBook.setDescription(超级酷的故事关于飞牛);
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 is created!,bookDao.findBookByIsbn10(0123456789),newBook);
}
}
所以当我运行该测试文件我得到以下错误:
Testcase:createBook(com.mysite.bookstore.tests.BookDaoTests):导致错误
null
java.lang.NullPointerException
在com.mysite.bookwebstore.beans.BookEJB.createBook(BookEJB.java:27)
在com.mysite.bookstore.tests .BookDaoTests.createBook(BookDaoTests.java:46)
EM:null
我使用以下技术:
- Glassfish 4
- JavaEE 7
- JSF
- EclipseLink 2.1
- Java DB
$ b $我希望我们能找到解决这个问题的一些解决方案。我已经解决了这个问题的3天,并从Google和Stackoverflow搜索并测试了解决方案,但没有一个解决方案有所帮助/有效。为了确保EntityManager真的为空,我调试了测试文件,看到它确实给了null。那么我该如何解决这个问题?
当EJB部署在容器中时,注入EntityManager实例。
如果您查看生命周期一个企业bean,你会看到清楚当依赖注入发生。
当容器看到@PersistenceContext注释时,它将注入一个容器管理的EntityManager。
问题是在本测试中执行的代码不是由因此,集装箱没有人注入必要的依赖关系。
bookDao = new BookEJB();
当您运行测试时,BookEJB类只是一个简单的POJO,@Stateless和@PersistenceContext注释只是被忽略。
为了测试你的EJB,你有几种方法,看看这个链接。
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.
Here is EJB class:
@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
}
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>
Here's my test file:
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
I use following technologies:
- Glassfish 4
- JavaEE 7
- JSF
- EclipseLink 2.1
- Java DB
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?
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.
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();
When you run the test, the BookEJB class is just a simple POJO, the @Stateless and @PersistenceContext annotations are simply ignored.
You have several alternatives in order to test your EJB, take a look at this link.
这篇关于EJB @PersistenceContext EntityManager抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!