我想用一个查询从三个表中删除相关数据。我收到错误消息: 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;
}
...
我现在不能再说了。有谁知道问题可能在哪里?
答案 0 :(得分:1)
1):您不会在此处传递false来打开交易:
startOperation(false);
2),您必须在finally块或try块结束之前提交它:
} finally {
getTx().commit();
getSession().close();
}