Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

Issue in persisting nested comments using Spring Hibernate

I am trying to create a simple CRUD application using Spring Boot with User, UserEntity, Post, Comment entities.
-> UserEntity is super class of Comment and Post.
-> Each comment has a ManyToOne relationship to a UserEntity (which can be a Post or another Comment)

UserEntity
???|
???@ManyToOne
???createdBy - refers to user table (id)
???|
--------------------
| ? ?????|
| ?????? |
Post ?? Comment
????????|
????????@ManytoOne
??????????UserEntity - refers to PK(entity_id) of user_entity table as comment can be on post or reply to another comment

On trying to save a comment on post from the CommentService class,

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

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

the foreign key in comment table (parent_entity_id) referring to entity_id in user_entity table is not getting updated. The value is blank.

On the other hand UserEntity has a manytoone relationship with User -- createdBy -- which is updating foriegn key user_id in user_entity table properly

Can someone guide me what could be wrong, I have been trying since yesterday night but no luck. Have checked some other answers but could not get an answer for this case.

User.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
}

UserEntity.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
}

Comment.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
}

and their repositories

@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> {

}

Json for postComment service

{
    "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
question from:https://stackoverflow.com/questions/66060767/issue-in-persisting-nested-comments-using-spring-hibernate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have very complex entity relationships, it seems to me...

Anyway, I found that you added a generator property to the UserEntity entity with a post_sequence value, but I can't find any relationship to the Post entity in your database. This is probably the reason of the breakdown. You have to connect UserEntity to Post as shown on your diagram or change the generator value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...