使用Spring Hibernate持久化嵌套注释的问题



我试图使用Spring Boot创建一个简单的CRUD应用程序与用户,UserEntity, Post,评论实体。
→UserEntity是Comment和Post的超类。
→每个评论都有一个与UserEntity(可以是Post或另一个评论)的ManyToOne关系

UserEntity
|
@ManyToOne
createdBy——是指用户表(id)
|
--------------------
|        |
|        |
文章评论
        |
        @ ManytoOne
           UserEntity——指PK (entity_id) user_entity表可以评论帖子或回复另一个评论

在试图从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

有谁能指导我什么可能是错的,我从昨天晚上就一直在尝试,但没有运气。我检查了一些其他的答案,但是没有得到这个问题的答案。

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
}

及其存储库

@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

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

无论如何,我发现您向具有post_sequence值的UserEntity实体添加了generator属性,但我无法在您的数据库中找到与Post实体的任何关系。这可能是故障的原因。您必须将UserEntity连接到Post,如图所示,或者更改生成器的值。

我能够解决这个问题。问题出现在注释具体类

中的以下代码段中
@ManyToOne
@JoinColumn(name="parent_entity_id", referencedColumnName = "entityId")
private UserEntity parentEntity;

和这个Json输入

"parentEntity" : {
"entityId" : 1
}

似乎json输入中的parentEntity没有被解析。在Json输入正确解析的情况下,将JsonProperty("parentEntity")置于parentEntity之上就解决了这个问题。

然而,还有一个问题。父实体没有被反序列化为UserEntity,因为UserEntity是一个抽象类。我必须通过引入一个新的字段parentType("P"对于post, "C">

public class Comment extends UserEntity{
private String comment;
@Transient
@JsonProperty("parentType")
private char parentType;
@ManyToOne
@JoinColumn(name="parent_entity_id", referencedColumnName = "entity_id", foreignKey = @ForeignKey(value=ConstraintMode.CONSTRAINT))
@JsonProperty("parentEntity")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME , property = "parentType", include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
@JsonSubTypes(value = {
@JsonSubTypes.Type(value = Comment.class, name = "C"),
@JsonSubTypes.Type(value = Post.class, name = "P")
})
private UserEntity parentEntity;

参考-杰克逊多态反序列化通过字段。我不太清楚这是怎么回事。我会试着弄明白并更新答案。

如果有人知道更好的反序列化json的方法,请在评论中提到它或作为一个新的答案。

最新更新