Spring Boot: "Cycling" in the request answer, when entities have bidirectional references (ManyToMany, FetchType.LAZY)

217 Views Asked by At

Good day,

I am studing the Spring Boot. Here is my prodject - https://github.com/Alex1182-St/java-spring-jpa-postgresql.git

I have 2 entities in it:

AppUserEntity - https://github.com/Alex1182-St/java-spring-jpa-postgresql/blob/master/src/main/java/com/java/javaspringjpapostgresql/entities/AppUserEntity.java

and RoleEntity - https://github.com/Alex1182-St/java-spring-jpa-postgresql/blob/master/src/main/java/com/java/javaspringjpapostgresql/entities/RoleEntity.java

The entities have bi-directional references (ManyToMany, FetchType.LAZY).

My problem is that when my method (method appUserById2WithPost in the AppUserController) returns an entity and not the DTO, then I receive a "cycling" in the answer. Here is how this cyckling looks like -

    "id": "e68e915f-e684-4b95-820a-a670a7bea677",
    "appUserLogin": "Login9",
    "appUserPassword": "$2a$10$ENyk.YDPLn4zsq1JL6Nol.97kwIlZVAK7pCei8I9i6LhzMj52UAN.",
    "roles": [
        {
            "id": "7a8abe8d-ab02-4e00-a463-a7d23df05778",
            "name": "USER",
            "appUsers": [
                {
                    "id": "e68e915f-e684-4b95-820a-a670a7bea677",
                    "appUserLogin": "Login9",
                    "appUserPassword": "$2a$10$ENyk.YDPLn4zsq1JL6Nol.97kwIlZVAK7pCei8I9i6LhzMj52UAN.",
                    "roles": [
                        {
                            "id": "7a8abe8d-ab02-4e00-a463-a7d23df05778",
                            "name": "USER",
                            "appUsers": [
                                {
                                    "id": "e68e915f-e684-4b95-820a-a670a7bea677",
                                    "appUserLogin": "Login9",
                                    "appUserPassword": "$2a$10$ENyk.YDPLn4zsq1JL6Nol.97kwIlZVAK7pCei8I9i6LhzMj52UAN.",
                                    "roles": [
                                        {
                                            "id": "7a8abe8d-ab02-4e00-a463-a7d23df05778",
                                            "name": "USER",
                                            "appUsers": [
                                                {
                                                    "id": "e68e915f-e684-4b95-820a-a670a7bea677",
                                                    "appUserLogin": "Login9",
                                                    "appUserPassword": "$2a$10$ENyk.YDPLn4zsq1JL6Nol.97kwIlZVAK7pCei8I9i6LhzMj52UAN.",
                                                    "roles": [
                                                        { AND SO ON...

In the Idea's console I have such mistake - Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]

Controller with the method - https://github.com/Alex1182-St/java-spring-jpa-postgresql/blob/master/src/main/java/com/java/javaspringjpapostgresql/controllers/AppUserController.java

Of course I can solve it by returning a DTO (like in the method appUserByIdWithPost) or by using annotations @JsonIdentityInfo in the Entities.

But I am afraid that a mistake will stiil be in the project. I want to solve it. But I cannot find what is the cause of it. Can someone help me? Or maybe such behaviour is normal?

Thank you in Advance.

1

There are 1 best solutions below

1
Mafor On

I would say this behavior is normal for bidirectional relationships and handling it with JSON annotations is fine (although not necessary with the @JsonIdentityInfo: you can consider JsonIgnore or JsonValue).

Another question is if you need the bidirectional relationship (is many-to-many on the RoleEntity really necessary?) or even if the RoleEntity should be an entity at all.