3

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.

Ozgur
  • 3,738
  • 17
  • 34

1 Answers1

2

On serializing Jackson will call the getter method which in return will retrieve the lazy loaded object using its proxy. if you check the generated sql you will see that no fortune_capability is being retrieved based on its user id on the findAll, if the fetch was eager you would see sql like this

Hibernate: select fortunet_capability ......... where fortunet_capability [some generated text].user_id=?
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • 1
    After a bit search based on your help I found out my problem was caused by Jackson library. (https://stackoverflow.com/questions/21708339/avoid-jackson-serialization-on-non-fetched-lazy-objects) – Ozgur Aug 13 '17 at 15:36