gpt4 book ai didi

java - 如何修复: javax. persistence.TransactionRequiredException?

转载 作者:行者123 更新时间:2023-11-30 05:36:48 26 4
gpt4 key购买 nike

我想通过一个查询删除三个表中的相关数据。我收到错误消息:javax.persistence.TransactionRequiredException:执行更新/删除查询

我们使用 java 8、Hibernate ORM 5.4.0 和 MySQL。当然,我已经看过这里但找不到适合我的问题的答案。 @Transactional 无法帮助我作为类或方法名称表示法。

这就是我的删除方法:

public void delteAllAccountsAndValues(Long accountIdToDelete) {
try {
startOperation(false);
getSession().createQuery("DELETE FROM AccountEntity WHERE accountId = :accountIdToDelete")
.setParameter("accountIdToDelete", accountIdToDelete)
.executeUpdate();
List accountLineIdsToDelete =
getSession()
.createQuery("SELECT ale.accountlineId FROM AccountlineEntity ale WHERE ale.accountId IN :accountIdToDelete")
.setParameter("accountIdToDelete", accountIdToDelete)
.list();
getSession().createQuery("DELETE FROM AccountlineEntity WHERE accountlineId = :accountLineIdsToDelete")
.setParameter("accountLineIdsToDelete", accountLineIdsToDelete)
.executeUpdate();
List accountLineValuesIdsToDelete =
getSession().createQuery("SELECT alve.accountlinevaluesId FROM AccountlinevaluesEntity alve WHERE" +
" alve.accountlineId IN :accountLineIdsToDelete")
.setParameter("accountLineIdsToDelete", accountLineIdsToDelete)
.list();
getSession().createQuery("DELETE FROM AccountlinevaluesEntity WHERE accountlinevaluesId = : accountLineValuesIdsToDelete")
.setParameter("accountLineValuesIdsToDelete", accountLineValuesIdsToDelete)
.executeUpdate();
} catch (HibernateException e) {
handleException(e);
} finally {
getSession().close();
}
}


我的 BaseManager 类的一部分:



void startOperation(boolean openTransaction) throws HibernateException {
this.session = HibernateUtil.getSessionFactory().openSession();
if (openTransaction) {
this.tx = session.beginTransaction();
}
}

void handleException(HibernateException e) {
System.out.println(e.getMessage());
if (e.getCause() != null) {
System.out.println(e.getCause().getMessage());
}
if (this.tx != null) {
this.tx.rollback();
}
}

protected Session getSession() {
return session;
}

protected void setSession(Session session) {
this.session = session;
}

protected Transaction getTx() {
return tx;
}

protected void setTx(Transaction tx) {
this.tx = tx;
}

public UserEntity getCurrentUser() {
return currentUser;
}

public void setCurrentUser(UserEntity currentUser) {
this.currentUser = currentUser;
}

这些实体在构造上都是相同的,因此以我的实体之一为例:

@Entity
@Table(name = "account")
public class AccountEntity {
@Expose() private Long accountId;
private String sourcedata;
private Timestamp dateCreated;
private Integer parentId;
@Expose() private MandantEntity mandantEntity;
@Expose() private UserEntity userEntity;


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "account_id", nullable = false)
public Long getAccountId() {
return accountId;
}

public void setAccountId(Long accountNewId) {
this.accountId = accountNewId;
}

@Basic
@Column(name = "sourcedata", nullable = false)
public String getSourcedata() {
return sourcedata;
}

public void setSourcedata(String sourcedata) {
this.sourcedata = sourcedata;
}

@Basic
@CreationTimestamp
@Column(name = "date_created")
public Timestamp getDateCreated() {
return dateCreated;
}

public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}

@Basic
@Column(name = "parent_id")
public Integer getParentId() {
return parentId;
}

public void setParentId(Integer parentId) {
this.parentId = parentId;
}

...



我现在无法进一步了解这一点。有谁知道问题到底出在哪里吗?

最佳答案

1) 您不会通过在此处传递 false 来打开交易:

startOperation(false);

2) 在finally block 中或try block 结束之前,您必须提交它:

    } finally {
getTx().commit();
getSession().close();
}

关于java - 如何修复: javax. persistence.TransactionRequiredException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56423665/

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