首页 > 解决方案 > 使用 Spring Hibernate 持久化嵌套注释的问题

问题描述

我正在尝试使用带有 User、UserEntity、Post、Comment 实体的 Spring Boot 创建一个简单的 CRUD 应用程序。
-> UserEntity 是 Comment 和 Post 的超类。
-> 每条评论与一个用户实体(可以是一个帖子或另一个评论)有一个​​多对一的关系

用户实体
   |
   @ManyToOne createdBy
   - 指用户表 (id)
   |
--------------------
| |
| |
发表评论
        |
        @ManytoOne
          UserEntity - 指 user_entity 表的 PK(entity_id),因为评论可以在帖子上或回复另一条评论

在尝试从 CommentService 类中保存对帖子的评论时,

//Controller
@PostMapping(path = "api/v1/addComment")
public void addComment(@RequestBody Comment comment){ commentService.addCommentOnPost(comment); }

//Service
public void addCommentOnEntity(Comment comment){ commentRepos.save(comment); }

注释表 (parent_entity_id) 中引用 user_entity 表中 entity_id 的外键未更新。该值为空白。

另一方面,UserEntity 与 User(createdBy)具有多对一关系,它正在正确更新 user_entity 表中的外键 user_id

有人可以指导我可能出了什么问题,我从昨天晚上开始一直在尝试,但没有运气。已经检查了其他一些答案,但无法得到这个案例的答案。

用户.java

@Entity
@Table(name="[user]")
public class User {
    @Id
    @SequenceGenerator(name="student_sequence",
    sequenceName = "student_sequence",
    allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
    generator = "student_sequence")
    private long id;
    private String name;
    private  String email;
    private int age;
    private LocalDate DOB;
//Setters and Getters and default constructor
}

用户实体.java

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class UserEntity {

    @Id
    @SequenceGenerator(sequenceName = "entity_sequence", name="entity_sequence", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_sequence")
    private long entityId;
    private char entityType;
    private LocalDate createdOn;
    private LocalDate modifiedOn;
    @ManyToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User createdBy;
//Setters and Getters and default constructor
}

Post.java

@Entity
public class Post extends UserEntity{

    private String postHeading;
    private String postBody;
//Setters and Getters and default constructor
}

评论.java

@Entity
public class Comment extends UserEntity{

    private String comment;
    @ManyToOne
    @JoinColumn(name="parent_entity_id", referencedColumnName = "entityId")
    private UserEntity parentEntity;
//Setters and Getters and default constructor
}

和他们的仓库

@NoRepositoryBean
public interface UserEntityBaseRepos<T extends UserEntity> extends JpaRepository<T, Long>{
        Optional<List<T>> findByCreatedBy_Id(Long user_id);
        Optional<List<T>> findByEntityId(Long entity_id);
}

@Repository
public interface UserRespository extends JpaRepository<User, Long> {
    Optional<User> findUserByEmail(String email);
    Optional<User> findUserByName(String name);
}

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

}

@Repository
public interface CommentRepos extends UserEntityBaseRepos<Comment>, JpaRepository<Comment, Long> {

}

用于 postComment 服务的 Json

{
    "entityType" : "C",
    "createdOn" : "2020-02-05",
    "createdBy" : {
        "id" : 1
    },

    "comment": "I am the comment",
    "parentEntity" : {
        "entityId" : 1
    }
}
//User with id = 1 and UserEntity(Post) with entityId = 1 available in database.
Here createdBy.id (user id) is getting updated in the user_entity table, but userEntity.entityId is not getting updated in the comment table

标签: springspring-boothibernatespring-data-jpa

解决方案


你有非常复杂的实体关系,在我看来......

无论如何,我发现您向具有值的实体添加了一个generator属性,但是我在您的数据库中找不到与该实体的任何关系。这大概就是崩盘的原因吧。您必须按照图表所示连接或更改生成器值。UserEntitypost_sequencePostUserEntityPost


推荐阅读