1

I've got two entities:

public class Rank{
     Short value; 

     @ManyToOne(fetch=FetchType.LAZY)
     Picture pic;
     ...
}


public class Picture{
      String url;
      Long rank;    // sum of all ranks' values
}

Is there a way to load Picture and rank field by query SELECT SUM(value) from ranks where pic = ??

4

1 に答える 1

0

Picture エンティティのランクのリストと、すべてのランクの合計を返すメソッドが必要です。

@Entity
public class Rank{
    Short value; 

    @ManyToOne(fetch=FetchType.LAZY)
    Picture pic;

    public void setPic(Picture p){
        pic = p;
        p.addRank(this);
    }

    ..
}


@Entity
public class Picture{
     String url;

     @OneToMany(fetch=FetchType.LAZY)
     List<Rank> ranks;

     Long rank;    // sum of all ranks' values

     public void addRank(Rank r) {
         ranks.add(r);
     }

     public Long getRank() {
         for(Rank r : this.ranks) {
             this.rank += r.getLongValue();
         }

         return this.rank;
     }

     ...
}
于 2013-01-24T17:19:54.347 に答える