我使用註釋@PersistenceUnit來獲取EntityManagerFactory的一個實例,但經過多次測試後,它不起作用。我一直在尋找理論,例子等,但沒有成功。理論看起來很簡單,但我無法看到我的代碼出現問題,或缺少什麼。@PersistenceUnit不工作(Java EE 7,Glassfish 4.1)
我使用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");
}
}
我想是不是按照理論的解決方案。我測試過了,它不起作用。看到這個[link](http://www.byteslounge.com/tutorials/container-vs-application-managed-entitymanager)。 – Manu
請再次閱讀您的鏈接...您未使用transaction-type =「RESOURCE_LOCAL」。 – unwichtich
如果我正確閱讀教程,則使用BMT_的_Valid應用程序託管持久性上下文使用建議代碼使用JTA事務類型。 – Manu