@PersistenceUnit不工作(Java EE 7,Glassfish 4.1)
我使用注释@PersistenceUnit获取EntityManagerFactory的一个实例,但经过几次测试后,它不起作用。我一直在寻找理论、例子等,但没有成功。这个理论看起来很简单,但我看不出我的代码哪里出了问题,或者缺少了什么 我正在使用的bean的代码是:@PersistenceUnit不工作(Java EE 7,Glassfish 4.1),java,persistence,jta,glassfish-4,Java,Persistence,Jta,Glassfish 4,我使用注释@PersistenceUnit获取EntityManagerFactory的一个实例,但经过几次测试后,它不起作用。我一直在寻找理论、例子等,但没有成功。这个理论看起来很简单,但我看不出我的代码哪里出了问题,或者缺少了什么 我正在使用的bean的代码是: import ... @Stateless @TransactionManagement(TransactionManagementType.BEAN) public class TopBean extends UserTrans
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;
}
}
使用@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");
}
}
我想问题是您正在尝试注入EntityManager工厂,但在使用JTA时必须注入EntityManager 将代码更改为如下所示:
@PersistenceUnit(unitName="puTop")
private EntityManager entityManager;
并直接使用EntityManager
如果改为使用事务类型RESOURCE_LOCAL,请将persistence.xml更改为以下内容:
<persistence-unit name="puTop" transaction-type="RESOURCE_LOCAL">
这就是坚持一个实体所需要的一切…根据理论,我认为这不是一个解决方案。我已经测试过了,但不起作用。请看这个。请再次阅读您的链接…您没有使用transaction type=RESOURCE\u LOCAL。如果我没有正确阅读教程,那么BMT有效应用程序管理的持久性上下文使用的建议代码使用JTA事务类型。我已将类UserTransactionWrapper的代码添加到原始问题中。在这里,您可以看到事务是如何以与教程中建议的解决方案相同的方式初始化的。我真的不明白您想做什么…请参阅我的更新。
<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;
}
}