java Hibernate软删除查询返回已删除的孙对象

t3irkdon  于 2024-01-05  发布在  Java
关注(0)|答案(3)|浏览(284)

在我的应用程序中的许多情况下,我希望使用@OneToMany和@ManyToOne关系返回数据树。我正在使用@SQLDelete @Where注解实现软删除。我不知道如何防止树返回软删除的孙对象。
例如,我的父实体…

  1. @Entity
  2. @Table(name = "gson_test_parent")
  3. @SQLDelete(sql = "UPDATE gson_test_parent SET deleted = true, deleted_at = now() WHERE id=?")
  4. @Where(clause = "deleted=false")
  5. public class GsonTestParent extends SoftDeletableEntity {
  6. public static final String STATUS_NEW = "new";
  7. public static final String STATUS_ACTIVE = "active";
  8. public static final String STATUS_DISABLED = "disabled";
  9. @Expose
  10. private String name;
  11. @Expose
  12. @OneToMany(fetch = FetchType.EAGER, mappedBy="gsonTestParentId")
  13. private List<GsonTestParentToGsonTestChild> gsonTestParentToGsonTestChild = new ArrayList<>();
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public List<GsonTestParentToGsonTestChild> getGsonTestParentToGsonTestChild() {
  21. return gsonTestParentToGsonTestChild;
  22. }
  23. }

字符串
.我的加入实体.

  1. @Entity
  2. @Table(name = "gson_test_parent_to_gson_test_child")
  3. @SQLDelete(sql = "UPDATE gson_test_parent_to_gson_test_child SET deleted = true, deleted_at = now() WHERE id=?")
  4. @Where(clause = "deleted=false")
  5. public class GsonTestParentToGsonTestChild extends SoftDeletableEntity {
  6. public static final String STATUS_ACTIVE = "active";
  7. public static final String STATUS_DISABLED = "disabled";
  8. @Expose
  9. private Long gsonTestParentId;
  10. @Expose
  11. @Transient
  12. @GsonExcludeBackReference
  13. private GsonTestParent gsonTestParent;
  14. @Expose
  15. @ManyToOne(fetch = FetchType.EAGER)
  16. @Where(clause = "deleted=false")
  17. private GsonTestChild gsonTestChild;
  18. public Long getGsonTestParentId() {
  19. return gsonTestParentId;
  20. }
  21. public GsonTestParent getGsonTestParent() {
  22. return gsonTestParent;
  23. }
  24. public void setGsonTestParent(GsonTestParent gsonTestParent) {
  25. this.gsonTestParent = gsonTestParent;
  26. }
  27. public GsonTestChild getGsonTestChild() {
  28. return gsonTestChild;
  29. }
  30. }


...和子实体...

  1. @Entity
  2. @Table(name = "gson_test_child")
  3. @SQLDelete(sql = "UPDATE gson_test_child SET deleted = true, deleted_at = now() WHERE id=?")
  4. @Where(clause = "deleted=false")
  5. public class GsonTestChild extends SoftDeletableEntity {
  6. public static final String STATUS_NEW = "new";
  7. public static final String STATUS_ACTIVE = "active";
  8. public static final String STATUS_DISABLED = "disabled";
  9. @Expose
  10. private String name;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }


仅供参考,这些都扩展了SoftDeletableEntity,看起来像...

  1. @MappedSuperclass
  2. public class SoftDeletableEntity extends BaseEntity {
  3. public SoftDeletableEntity() {
  4. super();
  5. }
  6. @Expose
  7. protected Timestamp deletedAt;
  8. protected Boolean deleted = Boolean.FALSE;
  9. public Timestamp getDeletedAt() {
  10. return deletedAt;
  11. }
  12. public void setDeletedAt(Timestamp deletedAt) {
  13. this.deletedAt = deletedAt;
  14. }
  15. public Boolean getDeleted() {
  16. return deleted;
  17. }
  18. public void setDeleted(Boolean deleted) {
  19. this.deleted = deleted;
  20. }
  21. }


当我找到父实体时...

  1. @GetMapping(path="/{id}")
  2. public ResponseEntity<String> get(@PathVariable Long id) throws BaseException {
  3. Map<String, Object> responseMap = new HashMap<>();
  4. GsonTestParent gsonTestParent = gsonTestParentService.find(id);
  5. responseMap.put("action", "Get");
  6. responseMap.put("message", "Entity retrieved");
  7. responseMap.put("entityType", "GsonTestParent");
  8. responseMap.put("entity", gsonTestParent);
  9. return responseService.success(responseMap);
  10. }


我得到子实体(孙子),即使它在数据库中被标记为已删除.

  1. {
  2. "payload": {
  3. "entityType": "GsonTestParent",
  4. "action": "Get",
  5. "message": "Entity retrieved",
  6. "entity": {
  7. "name": "test_parent_1",
  8. "gsonTestParentToGsonTestChild": [
  9. {
  10. "gsonTestParentId": 1,
  11. "gsonTestChild": {
  12. "name": "test_child_1",
  13. "deletedAt": "2022-07-26T04:31:30.000",
  14. "id": 1,
  15. "createdAt": "2022-07-22T07:24:15.000",
  16. "updatedAt": "2022-07-22T07:24:15.000",
  17. "status": "active"
  18. },
  19. "deletedAt": null,
  20. "id": 1,
  21. "createdAt": "2022-07-22T07:57:46.000",
  22. "updatedAt": "2022-07-22T07:57:46.000",
  23. "status": "active"
  24. }
  25. ],
  26. "deletedAt": null,
  27. "id": 1,
  28. "createdAt": "2022-07-22T07:23:15.000",
  29. "updatedAt": "2022-07-22T07:23:15.000",
  30. "status": "active"
  31. }
  32. },
  33. "status": "success"
  34. }


数据库中的gson_test_child记录

  1. mysql> select * from gson_test_child where id = 1;
  2. +----+---------------------+---------------------+---------------------+---------+--------+--------------+
  3. | id | created_at | updated_at | deleted_at | deleted | status | name |
  4. +----+---------------------+---------------------+---------------------+---------+--------+--------------+
  5. | 1 | 2022-07-22 14:24:15 | 2022-07-22 14:24:15 | 2022-07-26 11:31:30 | 1 | active | test_child_1 |
  6. +----+---------------------+---------------------+---------------------+---------+--------+--------------+


一些评论:

  • 我显式地引用连接表,而不是使用@JoinTable功能,因为我的应用程序中的许多“连接”表都有我想要公开的其他有意义的字段。
  • 我以为GsonTestParentToGsonTestChild.gsonTestChild字段上的@Where注解会消除软删除的子项,但显然不是(或者我做错了什么)。
  • 我知道我可以在存储库中创建显式的JOIN FETCH本地查询,它将过滤已删除的孙对象,但这就颠覆了使用注解的理由。

请让我知道如果我可以提供更多的信息。
迈克

6qfn3psc

6qfn3psc1#

尝试将@Where(clause = "deleted=false")也添加到集合中。

rseugnpd

rseugnpd2#

@Christian Beikov,
看起来你是对的。@Where(clause = "deleted=false")适用于@OneToMany关系,但不适用于@ManyToOne关系。

uurv41yg

uurv41yg3#

@Where(clause = "is_deleted = false")已弃用。您可以使用@SQLRestriction(value = "is_deleted = false")

相关问题