1

I have a Client.class which has a OneToMany relation with Posto.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;
    }

This was made because a use the Set in a responseEntity Controller and I need to preserve the order of how postiis displayed in the Json output.

So in Posto.class I implemented Comparable interface, overrding the compareTo method

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

Now, when calling my controller, i got this error from 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

How can i solve it? Hibernate changes my SortedSet in a PersistentSet, do I have to use this one to set my posti with the order i want?

1 Answer 1

1

The problem is you defined your posti and Luci as concrete SortSet. Hibernate PersistentSet implements generic Set interface. All you need to do is changing SortSet to generic Set and modify getters, setters accordingly.

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

Sign up to request clarification or add additional context in comments.

2 Comments

and i will preserve the order given by @OrderBy annotation when creating the JSON Object?
OrderBy: Specifies the ordering of the elements of a collection valued association or element collection at the point when the association or collection is retrieved. So, I think the order specified by OrderBy will be preserved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.