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": "[email protected]",
    "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.

6
  • how are you getting this json? Commented Aug 13, 2017 at 14:19
  • As I said through userRepository which extends CrudRepository using RestController. Commented Aug 13, 2017 at 14:20
  • when findAll is executed, check the generated sql, do you see a join? Commented Aug 13, 2017 at 14:22
  • No. It creates another selects to fetch data. Commented Aug 13, 2017 at 14:23
  • to fetch FortuneTeller ? Commented Aug 13, 2017 at 14:32

1 Answer 1

3

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=?
Sign up to request clarification or add additional context in comments.

1 Comment

After a bit search based on your help I found out my problem was caused by Jackson library. (stackoverflow.com/questions/21708339/…)

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.