我使用注释@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");
}
}
答案 0 :(得分:0)
我想问题是你试图注入EntityManagerFactory
,但是在使用JTA时你必须注入EntityManager
。
将您的代码更改为:
@PersistenceUnit(unitName="puTop")
private EntityManager entityManager;
并直接使用EntityManager
。
如果您想要使用事务类型&#34; RESOURCE_LOCAL&#34;然后将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;
}
}
这就是坚持实体所需的一切......