【发布时间】:2019-06-03 08:31:34
【问题描述】:
我想用一个查询从三个表中删除相关数据。我收到错误消息:javax.persistence.TransactionRequiredException: Executing an update/delete query
我们使用 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;
}
...
我现在不能再进一步了。有谁知道问题到底出在哪里?
【问题讨论】:
-
startOperation(false);- 您明确表示您不希望打开数据库事务。 -
谢谢!我错过了。 ....不,它有效... :)