내 엔티티에서 계산 된 필드, 즉 카운트를 처리하는 가장 좋은 방법에 대한 질문이 있습니다. 나는 리뷰를 포함하는 Establishment 엔티티를 가지고 있으며, 필자는 필 요할 때까지 리뷰를로드하고 싶지 않지만, 엔티티 오브젝트를로드 할 때 리뷰의 수를 얻고 싶습니다. 이것을 처리하는 가장 좋은 방법은 무엇입니까? numReviews 필드를 만들고 @Transient 주석을 달았지만 getReviews(). size()를 호출했을 때 세션이 닫히는 데 문제가 있습니다. 나는 이것이 올바른 접근법인지 또는 따라야 할 더 좋은 패턴이 있는지 알고 싶다.엔티티에서 계산 된 필드를 처리하는 방법
@Entity
@Table(name="ESTABLISHMENT")
public class Establishment {
...
private Set<Review> reviews = new HashSet<Review>();
...
//Make this extra lazy so we can do a count without loading all the things
@OneToMany(fetch = FetchType.LAZY, mappedBy = "establishment")
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<Review> getReviews() {
return reviews;
}
public void setReviews(Set<Review> reviews) {
this.reviews = reviews;
}
@Transient
public int getNumReviews(){
if(this.numReviews == null){
numReviews = this.getReviews().size();
}
return numReviews;
}