[英]Marshaling / Demarshaling JSON with Java EE 7 / Glassfish 4.1 results in null object
[英]@PersistenceUnit is not working (Java EE 7, Glassfish 4.1)
我使用批注@PersistenceUnit獲取EntityManagerFactory的實例,但經過多次測試后,它不起作用。 我一直在尋找理論,實例等,但是沒有成功。 理論上似乎很簡單,但是我看不到代碼的問題出在哪里,或者缺少什么。
我正在使用的bean的代碼是:
import ...
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class TopBean extends UserTransactionWrapper implements TopService
{
@Inject
Logger logger = LoggerFactory.getLogger(this.getClass());
@PersistenceUnit(unitName="puTop")
private EntityManagerFactory entityManagerFactory;
@Override
public OperationResult<Boolean> retrieve()
{
return execute();
}
protected OperationResult<Boolean> doRetrieve()
throws Exception
{
OperationResult<Boolean> operationResult = new OperationResult<Boolean>();
EntityManager entityManager = entityManagerFactory.createEntityManager();
long id = 5;
Node node = new Node(id, "Host.One", NodeType.SWITCH, true);
entityManager.persist(node);
operationResult.setData(node.getId() == id);
return operationResult;
}
@Override
protected Logger getLogger()
{
return logger;
}
}
UserTransactionWrapper類僅包含用於初始化由該函數獲得的用戶事務的代碼:
private UserTransaction getTransaction()
throws NamingException
{
Context context = new InitialContext();
return (UserTransaction)context.lookup("java:comp/UserTransaction");
}
用@Resource注入用戶事務不起作用,因此我必須這樣做。
我的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="puTop" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/Top</jta-data-source>
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
先感謝您!
馬努
我正在添加UserTransactionWrapper類的代碼:
public abstract class UserTransactionWrapper extends Wrapper
{
@SuppressWarnings("unchecked")
protected <E> OperationResult<E> execute(Object... parameters)
{
OperationResult<E> operationResult = new OperationResult<E>();
Method doMethod = findMethod();
Logger logger = getLogger();
// If the method exists...
if (doMethod != null)
{
UserTransaction userTransaction = null;
try
{
// Initializing user transaction
// =============================
userTransaction = getTransaction();
userTransaction.begin();
// Accomplishment of the operation
// ===============================
int parametersN = (parameters != null ? parameters.length : 0);
Object[] auxiliary = new Object[parametersN];
for (int i = parametersN; (--i) >= 0;) auxiliary[i] = parameters[i];
doMethod.setAccessible(true);
operationResult = (OperationResult<E>)doMethod.invoke(this, auxiliary);
// Completion of the transaction
// =============================
userTransaction.commit();
}
catch (Exception primary)
{
try
{
// If transaction is defined...
if (userTransaction != null) userTransaction.rollback();
boolean invocationError = primary instanceof InvocationTargetException;
// If the invoked method has thrown an exception...
if (invocationError)
{
Throwable cause = primary.getCause();
cause = (cause != null ? cause : primary);
operationResult.setError(cause);
logger.error(INVOCATION_ERROR, cause);
}
// If it hasn't done...
else
{
operationResult.setError(primary);
logger.error(UNEXPECTED_ERROR, primary);
}
}
catch (Exception secondary)
{
logger.error(UNEXPECTED_ERROR, secondary);
}
}
}
// If it doesn't exist...
else
{
operationResult = new OperationResult<E>();
operationResult.setError(new NoSuchMethodException());
}
return operationResult;
}
private UserTransaction getTransaction()
throws NamingException
{
Context context = new InitialContext();
return (UserTransaction)context.lookup("java:comp/UserTransaction");
}
}
我猜問題是您正在嘗試注入EntityManagerFactory
,但是在使用JTA時必須注入EntityManager
。
更改您的代碼,如下所示:
@PersistenceUnit(unitName="puTop")
private EntityManager entityManager;
並直接使用EntityManager
。
相反,如果要使用事務類型“ RESOURCE_LOCAL”,則將persistence.xml
更改為以下內容:
<persistence-unit name="puTop" transaction-type="RESOURCE_LOCAL">
無論如何,沒有理由進行此手動事務管理。
您可以這樣編寫代碼:
@Stateless
public class TopBean
{
@PersistenceUnit(unitName="puTop")
private EntityManager entityManager;
public Node persist() {
Node node = new Node(5, "Host.One", NodeType.SWITCH, true);
entityManager.persist(node);
return node;
}
}
這就是持久化實體所需的一切...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.