DeleteById在Spring Boot JPA存储库中不起作用

问题描述

我有两个实体-用户注释。一个用户可以有多个注释。我正在尝试对两个表实施软删除。对于User表,它工作正常,但对于Notes表,调用deleteById不会将Deleted列的值更改为true。我尝试返回findById(notesId),并且返回右行,但是删除不起作用。

package com.we.springmvcboot.Model;

import java.util.ArrayList;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import antlr.collections.List;

@Entity
@Table(name="User")
@SQLDelete(sql = "Update User set deleted = 'true' where UserID=?")
@Where(clause = "deleted = 'false'")//FALSE
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long UserID;
    
    @Column(name="emailid")
    private String emailID;

    @Column(name="deleted")
    private String deleted="false";
    
    @OneToMany(mappedBy="user",fetch = FetchType.EAGER,cascade=CascadeType.ALL,orphanRemoval=true)
    private Set<Notes> usernotes;
    
    public User() {}

    public User(String emailID) {
        super();
        this.emailID = emailID;
    }

    public String getDeleted() {
        return deleted;
    }

    public void setDeleted(String deleted) {
        this.deleted = deleted;
    }

    public long getUserID() {
        return UserID;
    }

    public void setUserID(long userID) {
        UserID = userID;
    }

    public String getemailID() {
        return emailID;
    }

    public void setemailID(String emailID) {
        this.emailID = emailID;
    }


    public Set<Notes> getUsernotes() {
        return usernotes;
    }

    public void setUsernotes(Set<Notes> usernotes) {
        this.usernotes = usernotes;
    }

}
package com.we.springmvcboot.Model;
    

import java.sql.Date;
import java.util.ArrayList;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
    
    @Entity
    @Table(name="Notes")
    @SQLDelete(sql = "Update Notes set deleted = 'true' where NotesID = ?")
    @Where(clause = "deleted = 'false'")
    public class Notes {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long NotesID;
        
        @Column(name="title")
        private String title;
        
        @Column(name="message")
        private String message;
        
        @Column(name="date")
        private String date;
        
        @Column(name="deleted")
        private String deleted="false";

        @Column(name="label")
        private int label=1;


        @ManyToOne()
        @JoinColumn(name = "UserID",nullable = false)


        private User user;

        
        public Notes() {}
        


        public Notes(String title,String message,String date,User user,int label) {
            super();
            this.title = title;
            this.message = message;
            this.date = date;
            this.user = user;
            this.label=label;
        }


        public Notes(long notesID,String title,int label) {
            super();
            NotesID = notesID;
            this.title = title;
            this.message = message;
            this.date = date;
            this.label=label;
        }


        public String getDeleted() {
            return deleted;
        }

        public void setDeleted(String deleted) {
            this.deleted = deleted;
        }

        public int getLabel() {
            return label;
        }

        public void setLabel(int label) {
            this.label = label;
        }

        public long getNotesID() {
            return NotesID;
        }


        public void setNotesID(long notesID) {
            NotesID = notesID;
        }


        public String getTitle() {
            return title;
        }


        public void setTitle(String title) {
            this.title = title;
        }


        public String getMessage() {
            return message;
        }


        public void setMessage(String message) {
            this.message = message;
        }


        public String getDate() {
            return date;
        }


        public void setDate(String date) {
            this.date = date;
        }



        public void setUser(User user) {
            this.user = user;
        }

            
    }
package com.we.springmvcboot.Service;

import com.we.springmvcboot.Model.*;
import com.we.springmvcboot.exception.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.we.springmvcboot.Repository.NotesRepository;
import com.we.springmvcboot.Repository.UserRepository;

@Service
public class TodoService {

    @Autowired
    UserRepository userrepo;

    @Autowired
    NotesRepository notesrepo;


  
    public Object deleteNote(Map<String,Object> input) throws InvalidInputException,NoteNotFoundException {
        long userID;
        try {
            userID = ((Number) input.get("userID")).longValue();
        } catch (Exception e) {
            throw new InvalidInputException("Missing UserID");
        }

        HashMap<String,Object> map = new HashMap<>();
        long notesID = ((Number) input.get("notesID")).longValue();

        System.out.println(notesID);

        if (!notesrepo.findById(notesID).isPresent())
            throw new NoteNotFoundException("Invalid Notes ID");
        
       **notesrepo.deleteById(notesID);**


        map.put("status",200);
        map.put("message","Request Successful");
        map.put("data",null);
        return map;
    }


    public Object deleteUser(Map<String,Object> input) throws NoteNotFoundException {
        HashMap<String,Object> map = new HashMap<>();
        long userID;
        userID = ((Number) input.get("userID")).longValue();
        if (!userrepo.findById(userID).isPresent())
            throw new NoteNotFoundException("Invalid User ID");
        userrepo.deleteById(userID);
        map.put("status",null);
        return map;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)