Reputation: 96
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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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;
Upvotes: 2
Views: 3394
Reputation: 96
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();
}
Upvotes: 1
Reputation: 601
Try annotating your save() method in blogService with @Transactional
Upvotes: 0