英文:
Spring Boot return null value for the relational entity after save
问题
我正在开发一个Spring Boot应用程序,我有两个实体AdminUsers和Blogs,这两个实体之间有OneToMany的关系。我想在保存博客后返回所有的博客,代码如下:
@PostMapping("/add")
@ResponseBody
public List<Blog> addBlog(@RequestBody Blog blog) {
Blog savedBlog = blogService.save(blog);
return blogService.findAllBlogs();
}
我面临的问题是,每次都会返回最后插入的博客的AdminUser为null,如下所示:
[
{
"id": 30,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": {
"adminid": 1,
"adminusername": "admin",
"adminemail": "admin@kongcepts.com",
"adminpassword": "$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC",
"status": 1,
"attempts": 0,
"resetToken": "$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa"
}
},
{
"id": 31,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": null
}
]
但是当我在此之后访问以下端点时:
@GetMapping("/get/blogs")
public List<Blog> listAllBlogs() {
return blogService.findAllBlogs();
}
它会返回预期的结果:
[
{
"id": 30,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": {
"adminid": 1,
"adminusername": "admin",
"adminemail": "admin@kongcepts.com",
"adminpassword": "$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC",
"status": 1,
"attempts": 0,
"resetToken": "$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa"
}
},
{
"id": 31,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": {
"adminid": 1,
"adminusername": "admin",
"adminemail": "admin@kongcepts.com",
"adminpassword": "$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC",
"status": 1,
"attempts": 0,
"resetToken": "$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa"
}
}
]
以下是我的AdminUser和Blog实体类:
@Entity
@Table(name = "tbl_admin")
public class AdminUser {
// ... 其他属性
@OneToMany(mappedBy = "adminUser")
@JsonIgnore
private List<Blog> blog;
@Entity
@Table(name = "tbl_blog")
public class Blog {
// ... 其他属性
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "admin_id", updatable = false, insertable = false)
private AdminUser adminUser;
}
英文:
I'm Working On a Spring Boot Application and I have two Entities AdminUsers And Blogs, These two Entities have OneToMany relationship and I wanted to Return all Blogs after save a blog, as follows
@PostMapping("/add")
@ResponseBody
public List<Blog> addBlog(@RequestBody Blog blog) {
Blog savedBlog = blogService.save(blog);
return blogService.findAllBlogs();
}
The problem I'm facing is tt returns null for the last inserted Blog's AdminUser all the time as follows,
[
{
"id": 30,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": {
"adminid": 1,
"adminusername": "admin",
"adminemail": "admin@kongcepts.com",
"adminpassword": "$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC",
"status": 1,
"attempts": 0,
"resetToken": "$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa"
}
},
{
"id": 31,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": null
}
]
But when I hit this endpoint after that
@GetMapping("/get/blogs")
public List<Blog> listAllBlogs() {
return blogService.findAllBlogs();
}
It returns as expected
[
{
"id": 30,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": {
"adminid": 1,
"adminusername": "admin",
"adminemail": "admin@kongcepts.com",
"adminpassword": "$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC",
"status": 1,
"attempts": 0,
"resetToken": "$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa"
}
},
{
"id": 31,
"adminid": 1,
"blogcontent": "blog dddcontent",
"blogtitle": "titleaa",
"datetime": "1111-12-31 23:59:59",
"adminUser": {
"adminid": 1,
"adminusername": "admin",
"adminemail": "admin@kongcepts.com",
"adminpassword": "$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC",
"status": 1,
"attempts": 0,
"resetToken": "$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa"
}
}
]
Here is my Entity classes for AdminUser and Blogs
Admin User
@Entity
@Table(name = "tbl_admin")
public class AdminUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "admin_id")
private Integer adminid;
@Column(name = "admin_username", nullable = false)
private String adminusername;
@Column(name = "admin_email", nullable = false)
private String adminemail;
@Column(name = "admin_password", nullable = false)
private String adminpassword;
@Column(name = "admin_status")
private Integer status = 1;
@Column(name = "admin_attempt")
private Integer attempts = 0;
@Column(name = "admin_reset_token")
private String resetToken;
@OneToMany(mappedBy = "adminUser")
@JsonIgnore
private List<Blog> blog;
and Blog
@Entity
@Table(name = "tbl_blog")
public class Blog {
@Id
@Column(name = "blog_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "admin_id")
private Integer adminid;
@Column(name = "blog_content")
private String blogcontent;
@Column(name = "blog_title")
private String blogtitle;
@Column(name = "blog_datetime")
private String datetime;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "admin_id", updatable = false, insertable = false)
private AdminUser adminUser;
答案1
得分: 1
我终于弄明白了,由于某些原因之前没有生效,因为我只是给名为adminid的连接列赋值 @JoinColumn(name = "admin_id", updatable = false, insertable = false)
,我想要做的只是搜索关联实体,使用以下代码:
AdminUser addedUser = adminService.findbyid(Integer id);
然后将结果添加到博客的关联实体(admin属性),所以最终的代码如下所示:
@PostMapping("/add")
@ResponseBody
public List<Blog> addBlog(@RequestBody Blog blog) {
AdminUser addedAdmin = adminService.findbyid(blog.getAdminid());
blog.setAdminuser(addedAdmin);
blogService.save(blog);
return blogService.findAllBlogs();
}
英文:
I finally figure it out that, for some reasons it didn't worked previously because i was only giving the value to the join column named adminid @JoinColumn(name = "admin_id", updatable = false, insertable = false)
, all i wanted to do search the relation entity with
AdminUser addedUser = adminService.findbyid(Integer id);
and added the result to the blog's relational entity(admin property),
so final code ends up with something like this.
@PostMapping("/add")
@ResponseBody
public List<Blog> addBlog(@RequestBody Blog blog) {
AdminUser addedAdmin = adminService.findbyid(blog.getAdminid());
blog.setAdminuser(addedAdmin);
blogService.save(blog);
return blogService.findAllBlogs();
}
答案2
得分: 0
尝试在blogService
的save()
方法上添加@Transactional
注解。
英文:
Try annotating your save() method in blogService with @Transactional
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论