我有这两节课:
@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());
}
}
相反,如果我执行此方法,它将从数据库中正确删除DinnerTable对象。
@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());
}
效果很好,实际上是从数据库中删除了DinnerTable对象:
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;