2016-03-30 11 views
1

私はPosto.classとOneToMany関係を持つClient.classを持っています。Spring + hibernate:予想される型:java.util.SortedSet、実際の値:org.hibernate.collection.internal.PersistentSet

@Entity 
@Table(name = "client", catalog = "SMARTPARK") 
public class Client implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private int idClient; 
    private String nomeClient; 
    private int numPosti; 
    private int numLuci; 
     private String currentIp; 

    private boolean online; 
    private String prop; 
    private SortedSet<Posto> posti = new TreeSet<>(); 
    private SortedSet<Luce> luci = new TreeSet<>(); 



    public Client() { 
    } 

    public Client(int idClient, String nomeClient, int numPosti, int numLuci, 
      String currentIp, boolean online, String prop, 
      SortedSet<Posto> posti, SortedSet<Luce> luci) { 
     this.idClient = idClient; 
     this.nomeClient = nomeClient; 
     this.numPosti = numPosti; 
     this.numLuci = numLuci; 

     this.currentIp = currentIp; 
     this.prop = prop; 
     this.online = online; 
     this.posti = posti; 
     this.luci = luci; 

    } 

    @Id 
    @Column(name = "id_client", unique = true, nullable = false) 
    public int getIdClient() { 
     return this.idClient; 
    } 

    public void setIdClient(int idClient) { 
     this.idClient = idClient; 
    } 

    @Column(name = "nome_client", nullable = false, length = 65535) 
    public String getNomeClient() { 
     return this.nomeClient; 
    } 

    public void setNomeClient(String nomeClient) { 
     this.nomeClient = nomeClient; 
    } 

    @Transient 
    public int getNumPosti() { 
     return this.numPosti; 
    } 

    public void setNumPosti(int numPosti) { 
     this.numPosti = numPosti; 
    } 

    @Transient 
    public int getNumLuci() { 
     return this.numLuci; 
    } 

    public void setNumLuci(int numLuci) { 
     this.numLuci = numLuci; 
    } 

    @Column(name = "client_ip", nullable=true) 
    public String getCurrentIp() { 
     return currentIp; 
    } 

    public void setCurrentIp(String currentIp) { 
     this.currentIp = currentIp; 
    } 

    @Column(name="online") 
    public boolean isOnline() { 
     return online; 
    } 

    public void setOnline(boolean online) { 
     this.online = online; 
    } 


    @Column(name="prop") 
    public String getProp() { 
     return prop; 
    } 

    public void setProp(String prop) { 
     this.prop = prop; 
    } 

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "client", orphanRemoval=true) 
    @OrderBy("numeroPosto ASC") 
    public Set<Posto> getPosti() { 
     return posti; 
    } 

    public void setPosti(SortedSet<Posto> posti) { 
     this.posti = posti; 
    } 

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "client", orphanRemoval=true) 
    @OrderBy("numeroLuce ASC") 
    public SortedSet<Luce> getLuci() { 
     return luci; 
    } 

    public void setLuci(SortedSet<Luce> luci) { 
     this.luci = luci; 
    } 

responseEntityコントローラでの使用を設定して、私はpostiは、JSONの出力に表示される方法の順序を保持する必要があるため、これが行われました。私は解決することができますどのように

2016-03-30 16:18:07.486 ERROR [http-nio-8080-exec-6]: HHH000123: IllegalArgumentException in class: it.besmart.models.Client, setter method of property: posti 
2016-03-30 16:18:07.486 ERROR [http-nio-8080-exec-6]: HHH000091: Expected type: java.util.SortedSet, actual value: org.hibernate.collection.internal.PersistentSet 

:私のコントローラを呼び出すとき

だからPosto.classで、私は今、compareToメソッド

@Override 
    public int compareTo(Posto o) { 
     if(this.numeroPosto == o.numeroPosto){ 
      return 0; 
     } else { 
      return this.numeroPosto > o.numeroPosto ? 1 : -1; 
     } 
をoverrding、Comparableインタフェースを実装し、私は、Hibernateから、このエラーを得ましたそれ? Hibernateは永続化セット内のSortedSetを変更します。私はこれを使用して私の望む順序でpostiを設定する必要がありますか?

答えて

1

問題は、postiとLuciを具象SortSetとして定義していることです。 Hibernate PersistentSetは、汎用のSetインターフェイスを実装しています。あなたがする必要があるのは、SortSetを汎用に変更することだけです。getter、setterを適切に設定して変更してください。

private Set<Posto> posti; private Set<Luce> luci;

+0

と私はJSONオブジェクトを作成するときに@OrderByアノテーションで指定された順序を維持するのだろうか? – besmart

+0

OrderBy:アソシエーションまたはコレクションが取得された時点で、コレクション値のアソシエーションまたはエレメントコレクションの要素の順序を指定します。 したがって、OrderByで指定された順序は保持されると思います。 –

関連する問題