gpt4 book ai didi

java - 为什么 JpaRepository 的删除方法实际上并没有从数据库中删除元素?

转载 作者:行者123 更新时间:2023-12-02 04:02:49 25 4
gpt4 key购买 nike

我有这两门课:

@Entity
public class Dinner {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long dinnerId;

@Column(unique = true)
private String date;

@OneToMany(mappedBy="dinner", orphanRemoval = true, cascade = CascadeType.PERSIST)
private List<DinnerTable> tables = new ArrayList<>();

@Entity
public class DinnerTable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long dinnerTableId;
private int places;

@ManyToOne
@JoinColumn(name = "dinner_id")
@JsonIgnore
private Dinner dinner;

如果我执行此操作,即使它为每个dinnerTable执行deleteById方法,它也不会删除任何内容。

@Override
public void test1(String date) {
Dinner dinner = dinnerService.getByDate(date);

for(DinnerTable table: dinner.getTables()){
dinnerTableRepository.deleteById(table.getDinnerTableId());
}
}

相反,如果我执行此方法,它会从数据库中正确删除 DiningTable 对象。

@Override
public void test2(String date) {
Dinner dinner = dinnerService.getByDate(date);

dinnerTableRepository.deleteById(1);
dinnerTableRepository.deleteById(2);
dinnerTableRepository.deleteById(3);
}

这些方法位于该服务类中

@Service
@Transactional
public class DinnerServiceImpl implements DinnerService {...}

DinnerTableRepository 就是这个类:

@Repository
public interface DinnerTableRepository extends JpaRepository<DinnerTable, Long> {
}

编辑

这也不起作用:

List<DinnerTable> tables = dinner.getTables();
for(int i = 1; i < 3; i++){
dinnerTableRepository.deleteById(tables.get(i).getDinnerTableId());
}

这工作正常,它实际上从数据库中删除了 DiningTable 对象:

long id = 40;  //id of the first DinnerTable to remove
for(int i = 1; i < 3; i++){
dinnerTableRepository.deleteById(id + i);
}

编辑2

这有效:

List<Long> ids = new ArrayList<>(Arrays.asList(43L, 44L, 45L));
for(Long id: ids){
dinnerTableRepository.deleteById(id);
}

编辑3

这样就可以了:

for(int i = 0; i < dinner.getTables().size(); i++){
dinner.removeTable(dinner.getTables().get(i));
}
dinnerRepository.save(dinner);

最佳答案

您在 @OneToMany@ManyToOne 中遇到问题。

此外,我正在使用 Project Lombok注释

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table

据我了解,Dinner-class 是主要对象,DinnerTable-class 是依赖对象。

在此示例中,我将使用@OneToMany/@ManyToOne双向

read this about cascade

试试这个代码:

Diner.java:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class Dinner {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dinner_generator")
@SequenceGenerator(name="dinner_generator", sequenceName = "dinner_seq", allocationSize = 1, initialValue = 1)
private long dinnerId;

@Column(unique = true)
private String date;

@OneToMany(
mappedBy = "professor",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<DinnerTable> tables = new ArrayList<>();

/*
Use methods below to add or remove tables!!!!!!
*/

public void addDinnertable(DinnerTable dinnerTable) {
tables.add(dinnerTable);
dinnerTable.setDinner(this);
}

public void removeDinnertable(DinnerTable dinnerTable) {
tables.remove(dinnerTable);
dinnerTable.setDinner(null);
}

DinnerTable.java:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class DinnerTable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dintable_generator")
@SequenceGenerator(name="dintable_generator", sequenceName = "dintable_seq", allocationSize = 1, initialValue = 1)
private Long id;

private long dinnerTableId;

private int places;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dinner_id")
private Dinner dinner;

关于java - 为什么 JpaRepository 的删除方法实际上并没有从数据库中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56725479/

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