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

时间:2019-06-23 15:47:29

标签: java spring hibernate jpa

我有这两节课:

List

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

@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对象。

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

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

这些方法在此服务类之内

@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 {...}

编辑

这也不起作用:

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

这很好,实际上它已从数据库中删除了DinnerTable对象:

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

编辑2

这有效:

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

编辑3

以这种方式起作用:

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

1 个答案:

答案 0 :(得分:0)

您在import tkinter as tk import requests API_ID = '///foo///' class GetWeather: def __init__(self, city, country): url_city = 'http://api.openweathermap.org/data/2.5/weather?q={},{}&appid={}'.format(city, country, API_ID) # note: save the weather data in 'self' self.weatherdata = requests.get(url_city).json() def print_coords(self): # get the stored weather data print(self.weatherdata) class MainApplication: def __init__(self, master): self.master = master self.frame = tk.Frame(self.master) self.button1 = tk.Button( self.frame, text='Coords', width=25, command=GetWeather('town', 'country').coords, ) self.button1.pack() self.frame.pack() def main(): root = tk.Tk() app = MainApplication(root) root.mainloop() if __name__ == "__main__": main() @OneToMany中遇到问题。

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

@ManyToOne

就我所知,晚餐类是主要对象,而晚餐表类是依赖的。

在此示例中,我将使用@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table 双向

read this about cascade

尝试以下代码:

Diner.java:

@OneToMany/@ManyToOne

DinnerTable.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);
    }