Spring + hibernate:预期类型:java.util.SortedSet,实际值:org.hibernate.collection.internal.PersistentSet

时间:2016-03-30 14:26:04

标签: java spring hibernate

我有一个Client.class,它与Posto.class有一个OneToMany关系。

@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 Controller中使用Set,我需要保留Json输出中posti的显示顺序。

所以在Posto.class中我实现了Comparable接口,覆盖了compareTo方法

@Override
    public int compareTo(Posto o) {
        if(this.numeroPosto == o.numeroPosto){
            return 0;
        } else {
            return this.numeroPosto > o.numeroPosto ? 1 : -1;
        }

现在,在调用我的控制器时,我从Hibernate得到了这个错误:

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

我该如何解决? Hibernate在PersistentSet中更改了我的SortedSet,我是否必须使用这个来设置我想要的命令?

1 个答案:

答案 0 :(得分:1)

问题是你将posti和Luci定义为具体的SortSet。 Hibernate PersistentSet实现了通用的Set接口。您需要做的就是将SortSet更改为泛型Set并相应地修改getter,setter。

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