3

I have the following in my code:

CompanyEntity

@Entity
@Table(name = "company")
public class Company{
   @OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
   @JsonUnwrapped
   private Set<User> users;
}

UserEntity

@Entity
@Table(name="user")
public class User{
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name="company_id")
    private Company company;
}

CompanyController

@GetMapping("/company")
public ResponseEntity<Object> getAllCompanies(){
    List<Company> allCompanies = companyService.findAll();
    return ResponseEntity.ok(allCompanies);

}

problem is when i call /company in the browser i am getting the users object including the company object. something like this

[
    {
        "id": 1,
        "name": "company",
        "users": [
            {
                "id": 14,
                "firstName": "Yamen",
                "lastName": "Nassif",
                "company": {
                    "id": 1,
                    "name": "company",
                    "users": [
                        {
                            "id": 14,
                            "firstName": "Yamen",
                            "lastName": "Nassif",
                            "company": {
                                "id": 1,
                                "name": "company",
                                "users": [

...

same goes when i getAllUsers companies and users are also exanding.

my database looks just fine.

and its endless and of course Stackoverflow error is in the console. How can i fix this ?

2 Answers 2

3

You can use @JsonIgnore annotation to prevent this type of behavior. This usually happens with bidirectional mapping within your entities. It is caused by infinite recursion.

@Entity
@Table(name="user")
public class User{
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name="company_id")
    @JsonIgnore
    private Company company;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have this error because of the infinite recursion.

Company has a link on User and User has a link on Company.

You have at least two options:

e.g.

@GetMapping("/company")
public ResponseEntity<Object> getAllCompanies() {
    List<Company> allCompanies = companyService.findAll();
    List<CompanyDto> allCompanyDtoList = convertToCompanyDtoList(allCompanies);
    return ResponseEntity.ok(allCompanyDtoList );
}

Personally, I'd prefer the second option, since returning Entities is NOT a good practice.

7 Comments

i tried something different: removing @OneToMany(mappedBy = "company", cascade = CascadeType.ALL) from Company isn`t working and then i have a persistence error
i mean removing the whole users object from Company, but something is getting broken.
and i am not sure but wont allCompanies already break or will already be expanded even without JSONing it ?
No, acquiring allCompanies with the code you've provided in the question won't break anything. This error is about infinite loop when serializing entities to JSON which you return from you rest controller.
ok thanks, i will try it. but how about removing one of the links from one entity? users from Company as an example ? how can i achieve that without an error ?
|

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.