我有一個戰爭文件運行在野蠻應用服務器上。JPA @EJB注入不起作用,並給NullPointerException
當我做一個SOAP請求時,發生一個servor日誌錯誤,說我的EJB會話爲空。這裏是我的代碼:
ColisDAO.java
@Stateless
@LocalBean
public class ColisDao {
public static final String SELECT_ALL_COLIS = "select * from Colis;";
@PersistenceContext(unitName="bdd_colis")
private EntityManager em;
public ColisDao() {
}
public void creer(Colis colis) {
...
}
public void remove(Colis colis) {
...
}
}
RecoveryImpl.java
public class RecoveryImpl {
@EJB
private static ColisDao colisDao;
public static void fillDataBase() throws IOException {
...
some code
...
colisDao.creer(c); //here's my error NullPointer
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="bdd_colis" transaction-type="JTA">
<jta-data-source>java:/bdd_colis</jta-data-source>
<class>data.Colis</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
我真的不明白我錯在哪裏,也許我必須創建ejb文件並使用函數,但我真的不知道如何在我的上下文中使用它。
有什麼建議嗎?
UPDATE
終於找到了解決辦法: 這是我的新RecoveryImpl類:
RecoveryImpl.java
@EJB(name="ejb/colisDao", beanInterface=ColisDao.class)
public class RecoveryImpl {
public static void fillDataBase() throws IOException {
try {
colisDao = (ColisDao) new InitialContext().lookup("ejb/colisDao");
}
colisDao.creer(c);
}
}
的[可能的複製是否有可能注入一個CDI Bean轉換成Java EE 6中的靜態變量?](https://stackoverflow.com/questions/19225179/is-it-possible-to-inject-a-cdi-bean-into-a-static-variable- in-java-ee-6) –
'colisDao'是靜態的 - 靜態字段不能注入CDI –
好的,謝謝你的回答,確實我看到別人的話題,但可用的解決方案仍然沒有工作。我會立即嘗試您的建議並儘快回覆。 –