gpt4 book ai didi

java - Spring + hibernate : Expected type: java. util.SortedSet,实际值: org. hibernate.collection.internal.PersistentSet

转载 作者:行者123 更新时间:2023-11-30 07:29:52 28 4
gpt4 key购买 nike

我有一个 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,并且我需要保留posti在Json输出中显示的顺序。

因此,在 Posto.class 中,我实现了 Comparable 接口(interface),覆盖了compareTo 方法

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

现在,当调用我的 Controller 时,我从 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?

最佳答案

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

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

关于java - Spring + hibernate : Expected type: java. util.SortedSet,实际值: org. hibernate.collection.internal.PersistentSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36311806/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com