gpt4 book ai didi

java.lang.IllegalArgumentException : Can not set int field it. besmart.models.Park.idPark 到 [Ljava.lang.Object;

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:11 36 4
gpt4 key购买 nike

我一直在解决这个问题。我有 3 个类(class)

User 和 Park 有 ManyToMany 关系,Piano 和 Park 有 ManyToOne 关系。

在模型中,有趣的部分是用户.java

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="users_parcheggio", joinColumns = {@JoinColumn(name="user_id") }, inverseJoinColumns = {
@JoinColumn(name="park_id") })
private Set<Park> parks = new HashSet<Park>();

这是Park.java

 @Entity
@Table(name="parcheggio", catalog="SMARTPARK")
public class Park implements Serializable{

/**
*
*/
private static final long serialVersionUID = -7630704706109692038L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_park", unique = true, nullable = false)
private int idPark;

@NotEmpty
@Column(name="nome_park")
private String nomePark;

@Column(name="descrizione")
private String descrizione;

@Column(name="indirizzo")
private String indirizzo;

@Column(name="citta")
private String citta;


@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="users_parcheggio", joinColumns = {@JoinColumn(name="park_id") }, inverseJoinColumns = {
@JoinColumn(name="user_id") })

private Set<User> users = new HashSet<User>();

@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy = "park")
private List<Piano> piano;

public Park(){
}

public Park(int idPark, String nomePark, String descrizione, String indirizzo, String citta, Set<User> users, List<Piano> piano){
this.idPark = idPark;
this.nomePark = nomePark;
this.descrizione = descrizione;
this.indirizzo = indirizzo;
this.citta = citta;
this.users = users;
this.piano = piano;
}

public List<Piano> getPiano() {
return piano;
}

public void setPiano(List<Piano> piano) {
this.piano = piano;
}

public int getIdPark() {
return idPark;
}

public void setIdPark(int idPark) {
this.idPark = idPark;
}


public String getNomePark() {
return nomePark;
}

public void setNomePark(String nomePark) {
this.nomePark = nomePark;
}


public String getDescrizione() {
return descrizione;
}

public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}

public String getIndirizzo() {
return indirizzo;
}

public void setIndirizzo(String indirizzo) {
this.indirizzo = indirizzo;
}

public String getCitta() {
return citta;
}

public void setCitta(String citta) {
this.citta = citta;
}

public Set<User> getUsers() {
return users;
}

public void setUsers(Set<User> users) {
this.users = users;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + idPark;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Park other = (Park) obj;
if (idPark != other.idPark)
return false;
return true;
}
}

在 Piano.java 中

@ManyToOne
@JoinColumn(name = "id_park")
public Park getPark() {
return park;
}

public void setPark(Park park) {
this.park = park;
}

当我成对使用这 3 个类(class)时(User with Park 或 Park with Piano)一切顺利,但现在我必须为给定用户获取所有钢琴。

在我的 PianoDaoImpl 中我有这个方法

public List<Piano> findPianoByUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName();
User user = userService.findBySso(name);
int userId = user.getId();
Query q = getSession().createQuery("from Park p join p.users u where u.id = :userId").setParameter("userId", userId);
List<Park> parks = q.list();

Query query = getSession().createQuery("from Piano p where p.park in :parks").setParameterList("parks", parks);
List piani = query.list();

return piani;
}

q 查询正确地为我提供了用户的公园列表。然后我将这个列表传递给第二个查询,我得到了这个异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of it.besmart.models.Park.idPark

造成

java.lang.IllegalArgumentException: Can not set int field it.besmart.models.Park.idPark to [Ljava.lang.Object;

我做错了什么?

最佳答案

问题的原因,即引用类型IntegeridPark不能被赋值给基本类型int的值。
尝试替换它。

    @Column(name = "id_park", unique = true, nullable = false)
private Integer idPark;

与:

   @Column(name = "id_park", unique = true, nullable = false)
private int idPark;

尝试检查 userId 的值是否为 Integer 而不是 int,因为我认为你有异常(exception):

  "from Park p join p.users u where u.id = :userId").setParameter("userId", userId);

关于java.lang.IllegalArgumentException : Can not set int field it. besmart.models.Park.idPark 到 [Ljava.lang.Object;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044978/

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