【问题标题】:Spring + hibernate : Expected type: java.util.SortedSet, actual value: org.hibernate.collection.internal.PersistentSetSpring + hibernate:预期类型:java.util.SortedSet,实际值:org.hibernate.collection.internal.PersistentSet
【发布时间】:2016-03-30 14:26:03
【问题描述】:

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

@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 控制器中使用 Set 并且我需要保留 posti 在 Json 输出中的显示顺序。

所以在 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,我是否必须使用这个来按照我想要的顺序设置我的 posti?

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

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

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

    【讨论】:

    • 我会在创建 JSON 对象时保留@OrderBy 注释给出的顺序?
    • OrderBy:指定在检索关联或集合时集合值关联或元素集合的元素的顺序。所以,我认为 OrderBy 指定的顺序会被保留。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 2021-05-28
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-12
    相关资源
    最近更新 更多