我一直在尋找很多類似的問題,這些問題並未反映我的確切問題。如果我忽視了某人已經解決了這個問題,請告訴我。EntityManager未注入無狀態會話Bean
我目前正在將JBoss 3.x上的舊的EJB 2.1應用程序遷移到JBoss 7.x上的EJB 3.x.由於應用程序客戶端和服務器之間的通信發生了變化,我創建了一個小型測試客戶端來檢查缺陷。
其中之一是,我的無狀態會話bean中的entitymanager沒有被注入,使EntityManager留空。我試着用EntityManagerFactory解決這個問題,但都沒有注入。 引用的數據源可用,連接已針對數據庫進行了測試。
我希望你們中的一些人遇到過這個問題,可以想到我可以在哪裏看下一個或我可以做什麼來跟蹤這個問題。 封閉的是來自服務器端的persistence.xml,RemoteInterface和Bean代碼以及我的測試客戶端代碼。
請讓我知道,如果我可以提供更多的信息。 (如果您想知道,這些類大多保留了EJB 2應用程序的名稱)。
無狀態會話Bean(GUITabUsersDao):
@Stateless
@Remote(GUITabUsersDaoRemote.class)
public class GUITabUsersDao implements GUITabUsersDaoRemote {
Logger logger = Logger.getLogger(GUITabUsersDao.class.getName());
@PersistenceContext(name = "OracleGUIDS", type = PersistenceContextType.TRANSACTION)
EntityManager entityManager;
@Override
public List<GUITabUsers> findAll() throws FinderException {
List<GUITabUsers> resultList = null;
TypedQuery<GUITabUsers> namedQuery;
if (entityManager == null) {
logger.severe("**** CKU: This is not going to end well. entitymanager is null. :(****");
} else {
namedQuery = entityManager.createNamedQuery(GUITabUsers.FIND_ALL, GUITabUsers.class);
resultList = namedQuery.getResultList();
}
return resultList;
}
}
遠程接口
public interface GUITabUsersDaoRemote {
List<GUITabUsers> findAll() throws FinderException;
}
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="OracleGUIDS" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/OracleGUIDS</jta-data-source>
<class>de.printcom.loginmodule.GUITabUsers</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="verify"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
客戶代碼
package de.my.test;
public class TestClient {
public static final String URL = "localhost";
public static final String PORT = "4447";
private static final Logger LOGGER = Logger.getLogger(TestClient.class.getName());
public static void main(String[] args) {
IDPLookupRemote bean;
LAEC_MachineControlSES bean2;
GUITabUsersDaoRemote guiTabUsersDao;
try {
EJBHomeFactory factory = EJBHomeFactory.getInstance(URL, PORT);
guiTabUsersDao = (GUITabUsersDaoRemote) lookup("GUITabUsersDao", GUITabUsersDaoRemote.class);
LOGGER.info("Bean '" + guiTabUsersDao.toString() + "' received.");
List<GUITabUsers> col = null;
try {
col = guiTabUsersDao.findAll();
} catch (FinderException | NullPointerException e) {
e.printStackTrace();
}
if (null != col) {
LOGGER.info(col.get(0).toString());
} else {
LOGGER.info("Col is empty!");
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Object lookup(String jndiName, Class<?> remoteInterfaceClass) throws NamingException {
Object object = new Object();
try {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put("jboss.naming.client.ejb.context", true);
properties.put(Context.PROVIDER_URL, "remote://" + URL + ":" + PORT);
Context context = new InitialContext(properties);
final String appName = "appName";
final String moduleName = "moduleName";
final String distinctName = "";
final String beanName = jndiName;
final String viewClassName = remoteInterfaceClass.getName();
LOGGER.info("Looking EJB via JNDI ");
String jndiLookupName = appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
LOGGER.info(jndiLookupName);
object = context.lookup(jndiLookupName);
LOGGER.info("Calling 'context.lookup(" + jndiLookupName + ")'");
// object = context.lookup(jndiName);
} catch (NamingException e) {
e.printStackTrace();
}
return object;
}
}
我不想問這個明顯的問題,但是你確定在啓動時成功創建了持久化上下文嗎?日誌中是否有錯誤? – jgitter
我很確定,因爲我可以在日誌文件顯示中沒有錯誤。此外,它聲明我的所有數據源都已部署。 另外我設置org.jboss.as.jpa爲TRACE日誌級別,希望能夠捕捉任何錯誤。 難道它不喜歡-ds.xml文件嗎? –
除了啓動JPA的日誌級別之外,還可以嘗試設置日誌級別以跟蹤執行情況。我假設你正在使用休眠? – jgitter