gpt4 book ai didi

java - Hibernate 多对一删除独生子

转载 作者:行者123 更新时间:2023-12-05 05:24:02 36 4
gpt4 key购买 nike

哪里有很多类似的话题,我自己还是搞不定,sorry。

所以,我有对象 "Bank",它是 "Office" 的父级。
我正在尝试删除单个 Office,但随后我删除了,父 bank 的所有 offices 都被删除了。
代码如下:
银行

@Entity
@Table(name = "BANKS")
public class Bank {
public Bank() {
}

public Bank(String name) {
this.name = name;
}

@Id
@Column(name = "ID")
@GeneratedValue
private int id;

@Column(name = "name")
private String name;

@OneToMany(mappedBy = "bank", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "BANK_ID")
private List<Office> officeList;

办公室

@Entity
@Table(name = "OFFICES")
public class Office {
public Office() {
}

public Office(String city, String address, String workingHours, Bank bank) {
this.city = city;
this.address = address;
this.workingHours = workingHours;
this.bank = bank;
}

@Id
@Column(name = "ID")
@GeneratedValue
private int id;

@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JoinColumn(name = "BANK_ID")
private Bank bank;

@Column(name = "CITY")
private String city;

@Column(name = "ADDRESS")
private String address;

@Column(name = "WORKING_HOURS")
private String workingHours;

OfficeDAO

@Repository
@Transactional
public class OfficeDAOImpl implements OfficeDAO {

@Autowired
private SessionFactory sessionFactory;

@Override
public Office getByOffice_ID(int Office_ID) {
return (Office) sessionFactory.getCurrentSession().get(Office.class, Office_ID);
}

@Override
public List<Office> getAllOffice() {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Office.class);
return criteria.list();
}

@Override
public int save(Office Office) {
return (Integer) sessionFactory.getCurrentSession().save(Office);
}

@Override
public void update(Office Office) {
sessionFactory.getCurrentSession().merge(Office);
}

@Override
public void view(Office Office) {
sessionFactory.getCurrentSession().merge(Office);
}

@Override
public void delete(int Office_ID) {
Office s = getByOffice_ID(Office_ID);
System.out.println("trying to delete" + Office_ID);
sessionFactory.getCurrentSession().delete(s);
}

}

Hibernate 查询跟踪然后仅删除办公室:

Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from offices where id=?
Hibernate: delete from banks where id=?

我做错了什么?任何建议将不胜感激。

更新

如果我从办公室删除cascade = CascadeType.REMOVE,hibernate 不会发送任何删除请求。

最佳答案

您应该以正确的方式配置CascadeType...

All - 不是您的最佳选择。尝试 DETACHREMOVE。它应该有帮助:)

更多关于 CascadeType can be found here .

关于java - Hibernate 多对一删除独生子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36058977/

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