Spring Boot在保存后为关联实体返回空值

huangapple go评论79阅读模式
英文:

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(&quot;/add&quot;)
    @ResponseBody
    public List&lt;Blog&gt; 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,

[
    {
        &quot;id&quot;: 30,
        &quot;adminid&quot;: 1,
        &quot;blogcontent&quot;: &quot;blog dddcontent&quot;,
        &quot;blogtitle&quot;: &quot;titleaa&quot;,
        &quot;datetime&quot;: &quot;1111-12-31 23:59:59&quot;,
        &quot;adminUser&quot;: {
            &quot;adminid&quot;: 1,
            &quot;adminusername&quot;: &quot;admin&quot;,
            &quot;adminemail&quot;: &quot;admin@kongcepts.com&quot;,
            &quot;adminpassword&quot;: &quot;$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC&quot;,
            &quot;status&quot;: 1,
            &quot;attempts&quot;: 0,
            &quot;resetToken&quot;: &quot;$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa&quot;
        }
    },
    {
        &quot;id&quot;: 31,
        &quot;adminid&quot;: 1,
        &quot;blogcontent&quot;: &quot;blog dddcontent&quot;,
        &quot;blogtitle&quot;: &quot;titleaa&quot;,
        &quot;datetime&quot;: &quot;1111-12-31 23:59:59&quot;,
        &quot;adminUser&quot;: null
    }
]

But when I hit this endpoint after that

 @GetMapping(&quot;/get/blogs&quot;)
public List&lt;Blog&gt; listAllBlogs() {
    return blogService.findAllBlogs();
}

It returns as expected

[

    {
        &quot;id&quot;: 30,
        &quot;adminid&quot;: 1,
        &quot;blogcontent&quot;: &quot;blog dddcontent&quot;,
        &quot;blogtitle&quot;: &quot;titleaa&quot;,
        &quot;datetime&quot;: &quot;1111-12-31 23:59:59&quot;,
        &quot;adminUser&quot;: {
            &quot;adminid&quot;: 1,
            &quot;adminusername&quot;: &quot;admin&quot;,
            &quot;adminemail&quot;: &quot;admin@kongcepts.com&quot;,
            &quot;adminpassword&quot;: &quot;$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC&quot;,
            &quot;status&quot;: 1,
            &quot;attempts&quot;: 0,
            &quot;resetToken&quot;: &quot;$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa&quot;
        }
    },
    {
        &quot;id&quot;: 31,
        &quot;adminid&quot;: 1,
        &quot;blogcontent&quot;: &quot;blog dddcontent&quot;,
        &quot;blogtitle&quot;: &quot;titleaa&quot;,
        &quot;datetime&quot;: &quot;1111-12-31 23:59:59&quot;,
        &quot;adminUser&quot;: {
            &quot;adminid&quot;: 1,
            &quot;adminusername&quot;: &quot;admin&quot;,
            &quot;adminemail&quot;: &quot;admin@kongcepts.com&quot;,
            &quot;adminpassword&quot;: &quot;$2a$10$3aVlo8BkGbKQYzMDMDZTi.6dQavPeY8j0yD833ldA4utNAE4SxzpC&quot;,
            &quot;status&quot;: 1,
            &quot;attempts&quot;: 0,
            &quot;resetToken&quot;: &quot;$2a$10$6o7N/u4pgwjWya30wueVve9oqPNO6TTLidOg6NincqL5lOvLh03oa&quot;
        }
    }
]

Here is my Entity classes for AdminUser and Blogs

Admin User

 @Entity
@Table(name = &quot;tbl_admin&quot;)
public class AdminUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;admin_id&quot;)
    private Integer adminid;

    @Column(name = &quot;admin_username&quot;, nullable = false)
    private String adminusername;

    @Column(name = &quot;admin_email&quot;, nullable = false)
    private String adminemail;

    @Column(name = &quot;admin_password&quot;, nullable = false)
    private String adminpassword;
    @Column(name = &quot;admin_status&quot;)
    private Integer status = 1;
    @Column(name = &quot;admin_attempt&quot;)
    private Integer attempts = 0;
    @Column(name = &quot;admin_reset_token&quot;)
    private String resetToken;

    @OneToMany(mappedBy = &quot;adminUser&quot;)
    @JsonIgnore
    private List&lt;Blog&gt; blog;

and Blog

@Entity
@Table(name = &quot;tbl_blog&quot;)
public class Blog {

    @Id
    @Column(name = &quot;blog_id&quot;)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = &quot;admin_id&quot;)
    private Integer adminid;

    @Column(name = &quot;blog_content&quot;)
    private String blogcontent;

    @Column(name = &quot;blog_title&quot;)
    private String blogtitle;

    @Column(name = &quot;blog_datetime&quot;)
    private String datetime;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = &quot;admin_id&quot;, 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 = &quot;admin_id&quot;, 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(&quot;/add&quot;)
    @ResponseBody
    public List&lt;Blog&gt; addBlog(@RequestBody Blog blog) {
        AdminUser addedAdmin = adminService.findbyid(blog.getAdminid());
        blog.setAdminuser(addedAdmin);
        blogService.save(blog);
        return blogService.findAllBlogs();

    }

答案2

得分: 0

尝试在blogServicesave()方法上添加@Transactional注解。

英文:

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

huangapple
  • 本文由 发表于 2020年4月3日 20:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61011929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定