Hibernate @OneToOne(fetch = FetchType.LAZY) is not working

Viewed 1541

User class

@Entity
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User extends BaseDomain {

    @Column(unique=true)
    private String email;
    private String name;
    private String surname;

    @JsonIgnore
    private String password;

    // fortune types
    @OneToOne(fetch = FetchType.LAZY)
    private FortuneTeller fortuneTeller;
    private int isFortuneTeller; // for efficient searching

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public String toString() {
        return "User{} " + super.toString();
    }

}

FortuneTeller:

@Entity
public class FortuneTeller extends FortuneCapability {

    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

}

FortuneCapability:

@Entity
@NoArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class FortuneCapability extends BaseDomain {

    private int totalFortune;

    private int price;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fortuneCapability")
    private List<Review> reviews = new ArrayList<>();

    public void addReview(Review review) {
        review.setFortuneCapability(this);
        reviews.add(review);
    }

    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }
}

When I fetch a user list gives me this json (I fetch them using userRepository.findAll();):

{
    "id": "4028ab6a5ddbc746015ddbc776580003",
    "createdAt": "13/08/2017",
    "updatedAt": "13/08/2017",
    "email": "asd@asd.com",
    "name": null,
    "surname": null,       
    "lastLogin": null,
    "fortuneTeller": {
        "id": "4028ab6a5ddbc746015ddbc7766f0006",
        "createdAt": "13/08/2017",
        "updatedAt": "13/08/2017",
        "totalFortune": 0,
        "price": 0,
        "reviews": [
            {
                "id": "4028ab6a5ddbc746015ddbc776710007",
                "createdAt": "13/08/2017",
                "updatedAt": "13/08/2017",
                "content": "asd",
                "rating": 0
            }
        ]
    },
    "isFortuneTeller": 1
}

Lazy loading is not working either for OneToOne or OneToMany. What can be the problem? I thought it's because of @Data annotation from Lombok and converted them to @Getter/Setter, but still same.

Also BaseDomain.java.

1 Answers
Related