@PersistenceContext为null Glassfish 4

时间:2014-07-02 14:57:04

标签: java jsf glassfish cdi

我正在使用JSF Glassfish 4和Eclipse

创建J2EE WebAPP

奇怪的是,我在两个不同的应用程序中执行相同的操作,在一个中工作,导致另一个中的NullPointerException。

我的ManagedBean是:

@ManagedBean(name = "showEntriesBean", eager = true)
@RequestScoped
public class ShowEntriesBean {
    @EJB
    EntryEAO eao;
    public List<Entry> getEntries() {
        return eao.getEntries();
    }

EntriesEAO是:

@Stateless
@LocalBean
public class EntryEAO {

@PersistenceContext(unitName = "LBBBankingWeb")
EntityManager em;
public final List<Entry> getEntries() {
            // This Line is leading to the NullPointerException an em is null
    final TypedQuery<Entry> query = em.createQuery("select r from " + Entry.class.getName() + " r order by r.valutadate", Entry.class);
    List<Entry> entries = query.getResultList();
    return entries;
}

的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="LBBBankingWeb">
        <jta-data-source>jdbc/lbb</jta-data-source>
        <class>de.docfaust.lbbweb.entity.Entry</class>
        <class>de.docfaust.lbbweb.entity.Rule</class>
        <class>de.docfaust.lbbweb.entity.Blockedrecipient</class>
        <properties>
            <property name="eclipselink.logging.level" value="FINE" />
        </properties>
    </persistence-unit>
</persistence>

当我将getEntries()代码直接放在ManagedBean中时,它可以工作。因此,持久性的配置似乎是正确的,所以我认为这是一个注入问题。

我已经阅读了许多关于@PersistenceContext和CDI的问题,还有一些是关于beans.xml的正确配置。

我实际上没有beans.xml,但有趣的是

  1. 在我的其他Webapp中我也没有,它可以工作;
  2. 像EntryEAO之类的其他注射工作。
  3. POM的依赖性

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
    

    感谢您的帮助

1 个答案:

答案 0 :(得分:0)

终于找到了答案:

这是在EAO

public final List<Entry> getEntries() {
    final TypedQuery<Entry> query = em.createQuery("select r from " + Entry.class.getName() + " r order by r.valutadate", Entry.class);
    List<Entry> entries = query.getResultList();
    return entries;
}

删除&#34; final&#34;修饰符一切正常

public List<Entry> getEntries() {
    TypedQuery<Entry> query = em.createQuery("select r from " + Entry.class.getName() + " r order by r.valutadate", Entry.class);
    List<Entry> entries = query.getResultList();
    return entries;
}