Hibernate从多个关联中删除实体
在我的数据库中,一篇文章可以分为许多类别,一个类别可能有许多文章。我有一个post和另一个category的关系。以下是他们的JPA映射: 邮政实体:Hibernate从多个关联中删除实体,hibernate,jpa,spring-data,persistence,Hibernate,Jpa,Spring Data,Persistence,在我的数据库中,一篇文章可以分为许多类别,一个类别可能有许多文章。我有一个post和另一个category的关系。以下是他们的JPA映射: 邮政实体: @ManyToMany(cascade = {PERSIST, MERGE}) private Set<Category> categories = new HashSet<>(); 我认为,如果在映射中使用联接表,就不会有问题。在这种情况下,一个给定的实体(比如,Post)将是关系的所有者(在其映射中有@JoinCol
@ManyToMany(cascade = {PERSIST, MERGE})
private Set<Category> categories = new HashSet<>();
我认为,如果在映射中使用联接表,就不会有问题。在这种情况下,一个给定的实体(比如,
Post
)将是关系的所有者(在其映射中有@JoinColumn
注释)。您只需通过以下方式即可实现您的目标:
entityManager.remove(post)
for (Category category : post.getCategories()) {
category.getPosts().remove(post);
}
链接,这些链接可能对您有所帮助:@CacheEvict(cacheNames = "pinnedPosts", allEntries = true)
public void deletePost(long postId) {
Optional<Post> optionalPost = postRepository.findById(postId);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
for (Category category : post.getCategories()) {
long newScore = post.getAuthor().getScore() - post.getLikesCount() * likeScore;
post.getAuthor().setScore(newScore);
category.getPosts().remove(post);
}
postRepository.deleteById(postId);
} else {
throw new PostNotFoundException();
}
}
...
Hibernate: delete from post_categories where posts_id=?
Hibernate: delete from post_categories where posts_id=?
Hibernate: delete from post_likers where favorites_id=?
Hibernate: delete from post_likers where favorites_id=?
Hibernate: delete from post where id=?
Hibernate: delete from post where id=?
2018-05-12 20:59:53.621 ERROR 14652 --- [nio-8000-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] with root cause
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3325) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3562) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:99) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:599) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:473) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
...
entityManager.remove(post)
for (Category category : post.getCategories()) {
category.getPosts().remove(post);
}