gpt4 book ai didi

java - EJB,持久化期间出现异常 : "EntityManager must be access within a transaction"

转载 作者:行者123 更新时间:2023-12-01 09:49:09 24 4
gpt4 key购买 nike

我收到此错误:“EntityManager 必须在事务内访问”这对我来说很奇怪,因为我的 bean 是用注释标记的:

@Stateless(name = "UserControllerSession")

我已经像这样注入(inject)了EntityManager:

@PersistenceContext(unitName = "app")
private EntityManager em;

这是我的方法:

 @Override
@PermitAll
public String updateBlockedAndDeactivationReasonForIds(String userPk, String iban, List<String> associatedIds) {
UserLocal user = em.find(UserLocal.class, Long.valueOf(userPk));
if (user == null) {
return ("User with id: " + userPk + " does not exist ! \n");
}

user.setBlocked(true);
user.setActive(false);
user.setDeactivationReason(DeactivationReason.DEACTIVATION_MULTIPLY_IBAN_REASON);
user.setDeactivationDate(new Date());

StringBuilder message = new StringBuilder();
message.append(" IBAN " + iban + " is linked to multiple accounts: ");
for (String id : associatedIds) {
message.append(id + " ");
}

ContactJournalEntryBean cjb = new ContactJournalEntryBean("USER", user.getLogin(), new Date(), "Internet",
message.toString());
em.persist(user);
em.persist(cjb);

return "SUCCESS operation for user with id: " + userPk;
}

据我所知,EJB 方法的默认值是 Transactioanl.REQUIRED,因此应该创建默认事务。另一个问题是我可以在一个事务中有 2 个持久化吗?感谢您的帮助

最佳答案

我认为默认的REQUIRED仅在您实际使用@TransactionAttribute注释时才适用。尝试将其添加到您的方法(或类,如果您希望将其作为所有方法的默认值)。

关于java - EJB,持久化期间出现异常 : "EntityManager must be access within a transaction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com