2

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;
3
  • can you share the exception? Commented Apr 6, 2020 at 13:17
  • it sounds like you miss/expect a cascade = CascadeTye.PERSIST (or more) in your Blog's @ManyToOne() annotation.? (vladmihalcea.com/…) Commented Apr 6, 2020 at 20:11
  • ...but the "updatable, insertable = false" is also somewhat strange there Commented Apr 6, 2020 at 20:18

2 Answers 2

1

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();

    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try annotating your save() method in blogService with @Transactional

1 Comment

Thank You For Answering, I Tried That But Still No Luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.