我有2个实体类,名为Post和Post详细信息。两者都使用MapsId
与OneToOne关系映射,并共享主键,如下所示。
Post.java
@Entity
@Table(name = "post")
@Data
public class Post implements Serializable
{
private static final long serialVersionUID = -6698422774799518217L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NaturalId
@Column(name = "title")
private String title;
@OneToOne(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
private PostDetail detail;
}
PostDetail.java
@Entity
@Table(name = "post_detail")
@Data
public class PostDetail implements Serializable
{
private static final long serialVersionUID = -6699482774799518217L;
@Id
private Long id;
@Column(name = "created_on")
private Date createdOn;
@Column(name = "created_by")
private String createdBy;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "id")
@JsonIgnore
private Post post;
}
PostController.java
@RestController
@RequestMapping("/api/v1/post")
public class PostController
{
private final PostRepository postRepository;
private final PostDetailRepository postDetailRepository;
public PostController(PostRepository postRepository, PostDetailRepository postDetailRepository)
{
this.postRepository = postRepository;
this.postDetailRepository = postDetailRepository;
}
@GetMapping(path = "/create")
public List<Post> createAndGetPosts()
{
Post post=new Post();
post.setId(new Random().nextLong());
post.setTitle("First Post");
post=postRepository.saveAndFlush(post);
PostDetail postDetail =new PostDetail();
postDetail.setCreatedBy("Admin");
postDetail.setCreatedOn(Date.from(Instant.now()));
postDetail.setPost(post);
postDetailRepository.saveAndFlush(postDetail);
return postRepository.findAll(Sort.by(Sort.Direction.DESC,"id"));
}
}
在Post Controller类中,我创建Post对象(保存DB),然后将其传递给PostDetail对象,然后使用Spring Data JPA将其保存到数据库中。一切都按预期工作。但是当我立即获取记录列表时,通过postReposity. findAll(Sort.by(Sort.Direction.DESC,"id"));
方法,我在Post中接收PostDetail对象的null
值,如下所示。
回应:
[
{
"id": 2,
"title": "First Post",
"detail": null
},
{
"id": 1,
"title": "Post1",
"detail": {
"id": 1,
"createdOn": "2019-06-21T03:31:43.000+0000",
"createdBy": "Admin"
}
}
]
但是当我再次从列表前端发送请求时,我得到了适当的响应。我尝试将flush语句和第二个findAll()语句放在请求之前,没有任何效果。
发生这种情况是因为您收到了从saveAndFlush
返回并存储在post
变量中的完全相同的实例。
当您执行postDetail. setPost(post)
时,Hibernate不会更新Post.详细
。
要修复它,您可以手动设置详细信息
或在保存后从缓存中驱逐Post
实例,强制Hibernate从DB重新加载它。