Abdul
Abdul

Reputation: 1208

Parent Entity is not populated in @OneToMany. Hibernate Bidirectional

hi newbie to Hibernate,

My entity classes

@Entity
public class User {

    @Id
    @GeneratedValue//How to restrcit by passing id from response
    @JsonIgnore
    private Integer userId;

    @OneToMany(mappedBy = "user")
    private List<PostEntity> postEntity;

}



@Entity
@Table(name="post")
public class PostEntity {

    @Id
    @GeneratedValue
    @JsonIgnore
    @ApiModelProperty(required=false)
    private Integer id;

    @ManyToOne(fetch=FetchType.LAZY)
    private User user;
}

When I fetch User its populating Post entity as well.

URI: http://localhost:8080/jpa/users

[
 {
     "name": "Abdul",
     "birthDate": "2018-07-25T01:29:51.895+0000",
     "postEntity": [
         {
             "description": "My Fourth Post"
         },
         {
             "description": "My First Post"
         },
         {
             "description": "My Second Post"
         }
     ]
 },
 {
     "name": "Anji",
     "birthDate": "2018-07-25T01:29:51.903+0000",
     "postEntity": []
 },
 {
     "name": "Naren",
     "birthDate": "2018-07-25T01:29:51.903+0000",
     "postEntity": []
 }
]

but this is not the case in reverse. When I fetch post its skipping User entity.

URI: localhost:8080/jpa/users/101/posts/11001 Response:

{
    "description": "My First Post"
}

Why its not populating user information in the above JSON response.

Fetching Methods:

User:

@GetMapping("/jpa/users")
public List<User> retAll(){
    return userRepository.findAll();
}

Post:

@GetMapping("/jpa/users/{uid}/posts/{pid}")
public Resource<PostEntity> postE(@PathVariable int uid,@PathVariable int pid) {
    Optional<PostEntity> post = postRepository.findById(pid);
    if (!post.isPresent()) {
        throw new UserNotFoundException("POst");
    }

    PostEntity ePost = post.get();
    Resource<PostEntity> resource = new Resource<PostEntity>(ePost);
    return resource;
}

Please help.

Upvotes: 1

Views: 590

Answers (2)

Bennett Dams
Bennett Dams

Reputation: 7033

That is actually the intended way REST is supposed to work.

GET at /users : all users

GET at /users/1 : information of user 1 and all its children

GET at /users/1/posts : all posts of user 1

GET at /users/1/posts/10 : information of post 10 and all its children from user 1

As you're calling /users/101/posts/11001, the endpoint will give you the information of one post (id 11001) from one user (id 101).

There are two common ways to get the parent information:

The fastest way would be to just call /users and filter for your desired post in the frontend.

The right way would be changing the model of your post (PostEntity.java) to contain its "parent" User object, so when you make a REST call for a post, the user object gets populated.

Further reading:

https://softwareengineering.stackexchange.com/questions/274998/nested-rest-urls-and-parent-id-which-is-better-design

Maybe it's a good idea to read some REST best practices:

https://hackernoon.com/restful-api-designing-guidelines-the-best-practices-60e1d954e7c9

Upvotes: 3

Oreste Viron
Oreste Viron

Reputation: 3805

Try to use FetchType

@Entity
public class User {

    @Id
    @GeneratedValue//How to restrcit by passing id from response
    @JsonIgnore
    private Integer userId;

    @OneToMany(mappedBy = "user", fetch=FetchType.EAGER)
    private List<PostEntity> postEntity;

}

Beware of the performance.

Upvotes: 2

Related Questions