用户类
@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();
}
}
算命先生:
@Entity
public class FortuneTeller extends FortuneCapability {
@Override
public String toString() {
return super.toString();
}
@Override
public boolean equals(Object o) {
return super.equals(o);
}
}
财富能力:
@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);
}
}
当我获取用户列表时,使用
userRepository.findAll();
命令后返回以下JSON格式数据:{
"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
}
无论是OneToOne还是OneToMany,懒加载都不起作用。可能的问题是什么?我以为这是由于Lombok中的@Data注解,所以将其转换为@Getter / @Setter,但仍然相同。
此外,请参见BaseDomain.java。
RestController
扩展CrudRepository
的userRepository
。 - Ozgur