简体   繁体   English

Spring JpaRepository 不会删除表中的行

[英]Spring JpaRepository doesn't delete row in table

I'm using postgresql and have 2 classes.我正在使用 postgresql 并有 2 个类。 User:用户:

@Entity
@Table(name="usr") //
public class User implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
}

And Post并发布

@Entity
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User author;
}

When I try to delete post by id Hibernate set all fields in null except user_id and not deleting.当我尝试通过 id 删除帖子时,Hibernate 将除 user_id 之外的所有字段设置为 null 并且不删除。 I've tried adding @Query in PostRepository我试着加入@Query在PostRepository

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(value = "DELETE FROM post WHERE id = ?1", nativeQuery = true)
void deleteById(long postId);

Tried adding @OneToMany in User.尝试在用户中添加@OneToMany Tried different FetchType, but nothing helps.尝试了不同的 FetchType,但没有任何帮助。

PostRepository:后存储库:

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

}

Delete post like this:像这样删除帖子:

@DeleteMapping("/{id}")
public String blogPostDelete(@PathVariable("id") long id) {
    postRepository.deleteById(id);
    return "redirect:/blog";
}

I'm almost shure there's something wrong in @ManyToOne but can't understand what exactly我几乎确定@ManyToOne 有问题,但不明白到底是什么

You can try to add @Transactional annotation above your custom query.您可以尝试在自定义查询上方添加@Transactional注释。
If result still will be the same you can find something in my working code.如果结果仍然相同,您可以在我的工作代码中找到一些内容。 Deleting was working:删除工作:

Parent:家长:

@Entity
@Table(name = "users")
public class EntityUser {

    @Id
    @Column(name = "uuid_user", length = 16, unique = true, nullable = false)
    private final UUID uuid = UUID.randomUUID();

    @OneToMany(targetEntity=EntityNote.class, mappedBy = "owner", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<EntityNote> noteList = new ArrayList<>();
}

And child:还有孩子:

@Entity
@Table(name = "notes")
public class EntityNote {
   
    @Id
    @Column(name = "uuid_note", length = 16, unique = true, nullable = false)
    private UUID uuid = UUID.randomUUID();
   
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner")
    private EntityUser owner;
}

You can try to do similar relation and check it will works and remodel for your requirements.您可以尝试做类似的关系并检查它是否有效并根据您的要求进行改造。

In your controller, try to fetch the record first using the 'id'在您的控制器中,尝试首先使用“id”获取记录

Post post = postRepository.findById(id)

then use the return Post object and call delete.然后使用返回 Post 对象并调用 delete。 No need to create such native query, JpaRepository (CrudRepository) has the basic built in functions you need不需要创建这样的原生查询,JpaRepository (CrudRepository) 有你需要的基本内置函数

postRepository.delete(post)

When working with Hibernate, it is interesting to put objects in session, whenever you want to perform any operation.使用 Hibernate 时,无论何时要执行任何操作,都可以将对象放入会话中。

So, try load you User object and remove the post with user.getListofPosts().remove(post.getId()).因此,尝试加载您的 User 对象并使用 user.getListofPosts().remove(post.getId()) 删除帖子。

After that, update your user object.之后,更新您的用户对象。

This should be done within your operation annotated with @Transactional.这应该在用@Transactional 注释的操作中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring JpaRepository不更新表 - Spring JpaRepository doesn't update table 为什么 JpaRepository 在 Spring Data Jpa 中没有 saveAll 方法? - Why doesn't JpaRepository have a saveAll method in Spring Data Jpa? JpaRepository插入无效[Spring Boot + MySQL] - JpaRepository insert doesn't work [Spring Boot + MySQL] Spring Boot JpaRepository保存调用似乎没有做任何事情 - Spring Boot JpaRepository save call doesn't seem to be doing anything Spring JpaRepository:delete(),在同一事务中使用后续的save() - Spring JpaRepository: delete() with subsequent save() in the same transaction SpringFox,Spring数据休息与JpaRepository - Swagger无法识别具有spring boot的Rest存储库 - SpringFox, Spring Data Rest with JpaRepository - Swagger doesn't recognize Rest repositories with spring boot 从 JpaRepository 检索 object 后,延迟加载在简单的 Hibernate/Spring 启动示例中不起作用 - Lazy loading doesn't work in simple Hibernate/Spring boot example, after retrieving object from JpaRepository Spring JpaRepository getOne()用于以PK作为字符串的表 - Spring JpaRepository getOne() for table with PK as String 如何使用Spring Boot JPARepository更新postgres表? - How to update postgres table with Spring Boot JPARepository? Spring不会注入JPARepository bean - Spring won't inject a JPARepository bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM