java.lang.IllegalArgumentException: 检索到的图书无效 Id:1
java.lang.IllegalArgumentException: Invalid retrievedBook Id:1
当我尝试使用此方法从数据库中删除具有关系的 Book 实体时
@Transactional
public void deleteBookById(Long id) {
List<BookStatus> bookStatuses = bookStatusService.findBookStatusesByBookId(id);
if (!bookStatuses.isEmpty()) {
bookStatusService.deleteBookStatusesByBookId(id);
}
bookRepository.delete(findBookById(id));
}
我收到一个错误
java.lang.IllegalArgumentException: Invalid retrievedBook Id:1
这里有更多日志,您可以在其中看到此错误恰好在我尝试使用休眠
删除实体时出现
java.lang.IllegalArgumentException: Invalid retrievedBook Id:1
at kpi.diploma.ovcharenko.service.book.LibraryBookService.lambda$findBookById[=13=](LibraryBookService.java:128) ~[classes/:na]
at java.base/java.util.Optional.orElseThrow(Optional.java:408) ~[na:na]
at kpi.diploma.ovcharenko.service.book.LibraryBookService.findBookById(LibraryBookService.java:128) ~[classes/:na]
at kpi.diploma.ovcharenko.service.book.LibraryBookService.deleteBookById(LibraryBookService.java:64) ~[classes/:na]
at kpi.diploma.ovcharenko.service.book.LibraryBookService$$FastClassBySpringCGLIB$c90428f.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.10.jar:5.3.10]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.10.jar:5.3.10]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.10.jar:5.3.10]
at kpi.diploma.ovcharenko.service.book.LibraryBookService$$EnhancerBySpringCGLIB$$e887da09.deleteBookById(<generated>) ~[classes/:na]
at kpi.diploma.ovcharenko.controller.BookController.deleteBookById(BookController.java:155) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.10.jar:5.3.10]
但我的数据库中有这个实体,其 ID 如下所示:
这是我的图书实体 class
@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idbooks", insertable = false, updatable = false, columnDefinition = "BINARY(16)")
private Long id;
@Column(name = "book_name")
@NotBlank(message = "Book name is mandatory")
private String bookName;
@Column(name = "year")
private int year;
@Column(name = "author")
private String author;
@Column(name = "amount")
private int amount;
@Column(name = "description")
private String description;
@Column(name = "section")
private String section;
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER)
@EqualsAndHashCode.Exclude
private Set<BookStatus> statuses = new HashSet<>();
@EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER)
private Set<BookCategory> categories = new HashSet<>();
@EqualsAndHashCode.Exclude
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<BookCard> bookCards = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
})
@JoinTable(
name = "books_tags",
joinColumns = {@JoinColumn(name = "book_id")},
inverseJoinColumns = {@JoinColumn(name = "book_tag_id")}
)
Set<BookTag> tags = new HashSet<>();
public void addCategory(BookCategory category) {
categories.add(category);
category.setBook(this);
}
public void removeCategory(BookCategory category) {
categories.remove(category);
category.setBook(null);
}
public void setStatus(BookStatus status) {
statuses.add(status);
status.setBook(this);
}
public Book(@NotBlank(message = "Book name is mandatory") String bookName, int year, String author,
int amount, String description) {
this.bookName = bookName;
this.year = year;
this.author = author;
this.amount = amount;
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return year == book.year && amount == book.amount && Objects.equals(id, book.id) && Objects.equals(bookName, book.bookName) && Objects.equals(author, book.author) && Objects.equals(description, book.description);
}
@Override
public int hashCode() {
return Objects.hash(id, bookName, year, author, amount, description);
}
}
这个错误太奇怪了,但我什至不知道我能做些什么来解决这个问题。
此处:
bookRepository.delete(findBookById(id));
您正试图将 findBookById(id)(属于 Optional 类型)传递给删除方法,实际上此方法是等待类型为 Long 而不是 Optional.
的参数
尝试通过直接传递 id 来删除图书:
bookRepository.delete(id);
先验证你传递的id是Long类型。
List<BookStatus> bookStatuses = bookStatusService.findBookStatusesByBookId(id);
if (!bookStatuses.isEmpty()) {
bookStatusService.deleteBookStatusesByBookId(id);
}
bookRepository.delete(findBookById(id));
在您上面的方法 (deleteBookById
) 中,您正在检索 bookStatuses
的列表,如果是 non-empty,您正在删除它。一旦它被删除,您将在上次调用 bookRepository.delete(findBookById(id));
.
中再次尝试删除它
可能这是您的问题。您能否检查 bookStatuses.isEmpty()
条件是否为真以及是否在 bookRepository.delete(findBookById(id));
调用之前发生删除?
对于以后也可能面临这样问题的人。我认为这个问题是由于 Hibernate 问题而出现的,因为我是这样解决的。
首先改变了我的 Book 实体 class
@Getter
@Setter
@Entity
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idbooks", insertable = false, updatable = false)
private Long id;
@Column(name = "book_name")
@NotBlank(message = "Book name is mandatory")
private String bookName;
@Column(name = "year")
private int year;
@Column(name = "author")
private String author;
@Column(name = "amount")
private int amount;
@Column(name = "description")
private String description;
@Column(name = "section")
private String section;
@EqualsAndHashCode.Exclude
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<BookStatus> statuses = new HashSet<>();
@EqualsAndHashCode.Exclude
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<BookCategory> categories = new HashSet<>();
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER, orphanRemoval = true)
private Set<BookCard> bookCards = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
})
@JoinTable(
name = "books_tags",
joinColumns = {@JoinColumn(name = "book_id")},
inverseJoinColumns = {@JoinColumn(name = "book_tag_id")}
)
Set<BookTag> tags = new HashSet<>();
public void addCategory(BookCategory category) {
categories.add(category);
category.setBook(this);
}
public void removeCategory(BookCategory category) {
categories.remove(category);
category.setBook(null);
}
public void addStatus(BookStatus status) {
statuses.add(status);
status.setBook(this);
}
public Book(@NotBlank(message = "Book name is mandatory") String bookName, int year, String author,
int amount, String description) {
this.bookName = bookName;
this.year = year;
this.author = author;
this.amount = amount;
this.description = description;
}
}
之后,我在我的 BookRepository 接口中将默认的 Hibernate 方法 deleteById() 更改为此
@Modifying
@Transactional
@Query(value = "DELETE FROM Book b WHERE b.id = :id")
void deleteBookById(@Param("id") Long id);
在我的 BookService 方法 deleteBookId() 中,我删除了删除 BookStatus,因为当我的书成功删除时,BookStatuses 会级联删除。并添加方法,通过图书 id
删除所有图书卡片
@Override
@Transactional
public void deleteBookById(Long bookId) {
bookCardService.deleteAllBookCardsByBookId(bookId);
bookRepository.deleteBookById(bookId);
}
这里是BookCard仓库界面的deleteAllBookCardsByBookId
@Modifying
@Query("delete from BookCard bc where bc.book.id = :id")
void deleteBookCardsByBookId(Long id);
在所有这些更改之后,我解决了我的问题并成功删除了图书实体。
真心希望以后我解决这个问题的方法对遇到和我一样的人有用
当我尝试使用此方法从数据库中删除具有关系的 Book 实体时
@Transactional
public void deleteBookById(Long id) {
List<BookStatus> bookStatuses = bookStatusService.findBookStatusesByBookId(id);
if (!bookStatuses.isEmpty()) {
bookStatusService.deleteBookStatusesByBookId(id);
}
bookRepository.delete(findBookById(id));
}
我收到一个错误
java.lang.IllegalArgumentException: Invalid retrievedBook Id:1
这里有更多日志,您可以在其中看到此错误恰好在我尝试使用休眠
java.lang.IllegalArgumentException: Invalid retrievedBook Id:1
at kpi.diploma.ovcharenko.service.book.LibraryBookService.lambda$findBookById[=13=](LibraryBookService.java:128) ~[classes/:na]
at java.base/java.util.Optional.orElseThrow(Optional.java:408) ~[na:na]
at kpi.diploma.ovcharenko.service.book.LibraryBookService.findBookById(LibraryBookService.java:128) ~[classes/:na]
at kpi.diploma.ovcharenko.service.book.LibraryBookService.deleteBookById(LibraryBookService.java:64) ~[classes/:na]
at kpi.diploma.ovcharenko.service.book.LibraryBookService$$FastClassBySpringCGLIB$c90428f.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.10.jar:5.3.10]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.10.jar:5.3.10]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.10.jar:5.3.10]
at kpi.diploma.ovcharenko.service.book.LibraryBookService$$EnhancerBySpringCGLIB$$e887da09.deleteBookById(<generated>) ~[classes/:na]
at kpi.diploma.ovcharenko.controller.BookController.deleteBookById(BookController.java:155) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.10.jar:5.3.10]
但我的数据库中有这个实体,其 ID 如下所示:
@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idbooks", insertable = false, updatable = false, columnDefinition = "BINARY(16)")
private Long id;
@Column(name = "book_name")
@NotBlank(message = "Book name is mandatory")
private String bookName;
@Column(name = "year")
private int year;
@Column(name = "author")
private String author;
@Column(name = "amount")
private int amount;
@Column(name = "description")
private String description;
@Column(name = "section")
private String section;
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER)
@EqualsAndHashCode.Exclude
private Set<BookStatus> statuses = new HashSet<>();
@EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER)
private Set<BookCategory> categories = new HashSet<>();
@EqualsAndHashCode.Exclude
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<BookCard> bookCards = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
})
@JoinTable(
name = "books_tags",
joinColumns = {@JoinColumn(name = "book_id")},
inverseJoinColumns = {@JoinColumn(name = "book_tag_id")}
)
Set<BookTag> tags = new HashSet<>();
public void addCategory(BookCategory category) {
categories.add(category);
category.setBook(this);
}
public void removeCategory(BookCategory category) {
categories.remove(category);
category.setBook(null);
}
public void setStatus(BookStatus status) {
statuses.add(status);
status.setBook(this);
}
public Book(@NotBlank(message = "Book name is mandatory") String bookName, int year, String author,
int amount, String description) {
this.bookName = bookName;
this.year = year;
this.author = author;
this.amount = amount;
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return year == book.year && amount == book.amount && Objects.equals(id, book.id) && Objects.equals(bookName, book.bookName) && Objects.equals(author, book.author) && Objects.equals(description, book.description);
}
@Override
public int hashCode() {
return Objects.hash(id, bookName, year, author, amount, description);
}
}
这个错误太奇怪了,但我什至不知道我能做些什么来解决这个问题。
此处:
bookRepository.delete(findBookById(id));
您正试图将 findBookById(id)(属于 Optional 类型)传递给删除方法,实际上此方法是等待类型为 Long 而不是 Optional.
的参数尝试通过直接传递 id 来删除图书:
bookRepository.delete(id);
先验证你传递的id是Long类型。
List<BookStatus> bookStatuses = bookStatusService.findBookStatusesByBookId(id);
if (!bookStatuses.isEmpty()) {
bookStatusService.deleteBookStatusesByBookId(id);
}
bookRepository.delete(findBookById(id));
在您上面的方法 (deleteBookById
) 中,您正在检索 bookStatuses
的列表,如果是 non-empty,您正在删除它。一旦它被删除,您将在上次调用 bookRepository.delete(findBookById(id));
.
可能这是您的问题。您能否检查 bookStatuses.isEmpty()
条件是否为真以及是否在 bookRepository.delete(findBookById(id));
调用之前发生删除?
对于以后也可能面临这样问题的人。我认为这个问题是由于 Hibernate 问题而出现的,因为我是这样解决的。 首先改变了我的 Book 实体 class
@Getter
@Setter
@Entity
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idbooks", insertable = false, updatable = false)
private Long id;
@Column(name = "book_name")
@NotBlank(message = "Book name is mandatory")
private String bookName;
@Column(name = "year")
private int year;
@Column(name = "author")
private String author;
@Column(name = "amount")
private int amount;
@Column(name = "description")
private String description;
@Column(name = "section")
private String section;
@EqualsAndHashCode.Exclude
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<BookStatus> statuses = new HashSet<>();
@EqualsAndHashCode.Exclude
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<BookCategory> categories = new HashSet<>();
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "book", fetch = FetchType.EAGER, orphanRemoval = true)
private Set<BookCard> bookCards = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
})
@JoinTable(
name = "books_tags",
joinColumns = {@JoinColumn(name = "book_id")},
inverseJoinColumns = {@JoinColumn(name = "book_tag_id")}
)
Set<BookTag> tags = new HashSet<>();
public void addCategory(BookCategory category) {
categories.add(category);
category.setBook(this);
}
public void removeCategory(BookCategory category) {
categories.remove(category);
category.setBook(null);
}
public void addStatus(BookStatus status) {
statuses.add(status);
status.setBook(this);
}
public Book(@NotBlank(message = "Book name is mandatory") String bookName, int year, String author,
int amount, String description) {
this.bookName = bookName;
this.year = year;
this.author = author;
this.amount = amount;
this.description = description;
}
}
之后,我在我的 BookRepository 接口中将默认的 Hibernate 方法 deleteById() 更改为此
@Modifying
@Transactional
@Query(value = "DELETE FROM Book b WHERE b.id = :id")
void deleteBookById(@Param("id") Long id);
在我的 BookService 方法 deleteBookId() 中,我删除了删除 BookStatus,因为当我的书成功删除时,BookStatuses 会级联删除。并添加方法,通过图书 id
删除所有图书卡片@Override
@Transactional
public void deleteBookById(Long bookId) {
bookCardService.deleteAllBookCardsByBookId(bookId);
bookRepository.deleteBookById(bookId);
}
这里是BookCard仓库界面的deleteAllBookCardsByBookId
@Modifying
@Query("delete from BookCard bc where bc.book.id = :id")
void deleteBookCardsByBookId(Long id);
在所有这些更改之后,我解决了我的问题并成功删除了图书实体。 真心希望以后我解决这个问题的方法对遇到和我一样的人有用