[英]Spring Boot Jpa Fetch Relations Not Working After Save Entity
I have a project with spring boot and hibernate.Everything working well with select data from database but I have a problem after insert entities.我有一个带有spring boot和hibernate的项目。从数据库中选择数据一切正常,但插入实体后出现问题。 Entity relations not fetch after insert.插入后不获取实体关系。 I tried JpaRepository saveAndFlush
but didn't work.Also I tried findById
didn't work .我试过 JpaRepository saveAndFlush
但没用。我也试过findById
。 How can I solve this problem ?我怎么解决这个问题 ?
My Entity;我的实体;
@Entity
@Table(name = "Comment")
@Data
@ApiModel(value = "Comment Model")
public class Comment {
public static Comment createCommentFromCommentPostRequest(CommentPostRequest commentPostRequest){
Comment comment = new Comment();
comment.setPostId(commentPostRequest.getPostId());
comment.setUsersId(commentPostRequest.getUsersId());
comment.setText(commentPostRequest.getText());
return comment;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@ApiModelProperty(value = "Comment model autogenerated Id value")
private Long id;
@Column(name = "usersId",nullable = false)
@ApiModelProperty(value = "User Id for describe who commented post")
private Long usersId;
@Column(name = "postId",nullable = false)
@ApiModelProperty(value = "Post Id for describe which post commented")
private Long postId;
@Column(name = "text",length = 500)
@ApiModelProperty(value = "define post content")
private String text;
@ManyToOne(fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "usersId",insertable = false,updatable = false,nullable = false)
private Users users;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "postId",insertable = false,updatable = false,nullable = false)
@JsonIgnore
@OnDelete(action = OnDeleteAction.CASCADE)
private Post post;
}
My Repository;我的存储库;
public interface CommentRepository extends PagingAndSortingRepository<Comment,Long> {
List<Comment> findAllByPostId(Long postId, Pageable pageable);
}
My Service Method;我的服务方式;
public Comment saveOneComment(CommentPostRequest commentPostRequest) {
//TODO: implement validation
Comment commentToSave = Comment.createCommentFromCommentPostRequest(commentPostRequest);
commentToSave= commentRepository.save(commentToSave);
//I tried JPA repository saveAndFlush
return commentRepository.findById(commentToSave.getId()).orElse(null);
}
My Controller;我的控制器; using saveOneComment endpoint使用 saveOneComment 端点
@RestController
@RequestMapping("/api/comment")
@ApiOperation(value = "Comment api controller")
public class CommentController {
CommentService commentService;
public CommentController(CommentService commentService) {
this.commentService = commentService;
}
@GetMapping("/{pageNo}/{pageSize}")
public List<Comment> getAllComments(@PathVariable int pageNo,@PathVariable int pageSize){
return Streamable.of(commentService.getAllComments(pageNo,pageSize)).toList();
}
@GetMapping("/{commentId}")
public Comment findCommentById(@PathVariable Long commentId){
return commentService.findById(commentId);
}
@GetMapping("/postId={postId}/{pageNo}/{pageSize}")
public List<CommentResponse> getAllCommentsByPostId(@PathVariable Long postId,@PathVariable int pageNo,@PathVariable int pageSize){
return commentService.getAllCommentsByPostIdAsCommentResponse(postId,pageNo,pageSize);
}
@PostMapping
public Comment saveOneComment(@RequestBody CommentPostRequest commentPostRequest){
return commentService.saveOneComment(commentPostRequest);
}
@PutMapping("/{commentId}")
public Comment putOneComment(@PathVariable Long commentId,@RequestBody CommentPostRequest commentPostRequest){
return commentService.putOneCommentById(commentId,commentPostRequest);
}
@DeleteMapping("/{commentId}")
public void deleteOneComment(@PathVariable Long commentId){
commentService.deleteById(commentId);
}
}
Comment(id=32, usersId=2, postId=2, text=ddwad, users=null, post=null)
After insert and find again, users and post is null. Comment(id=32, usersId=2, postId=2, text=ddwad, users=null, post=null)
再次插入查找后,users和post为null。 When I run getAllCommentsByPostId method everything is fine.当我运行 getAllCommentsByPostId 方法时,一切都很好。
after insert only insert query in console.There is not any query for select statement.插入后仅在控制台中插入查询。选择语句没有任何查询。
insert
into
Comment
(postId, text, usersId)
values
(?, ?, ?)
you have to reload the object again after committing the transaction, i mean outside the transaction to maintain the relations您必须在提交事务后再次重新加载对象,我的意思是在事务之外维护关系
@PostMapping
public Comment saveOneComment(@RequestBody CommentPostRequest commentPostRequest){
Long id= commentService.saveOneComment(commentPostRequest);
return commentService.loadCommentById(id);
}
````
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.