User和Park有ManyToMany关系,Piano和Park有ManyToOne关系。
在模型中,有趣的部分是 User.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;
}
当我在情侣中使用这三个课程(Park 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;
我做错了什么?
答案 0 :(得分:2)
问题的原因,即引用类型idPark
的{{1}},无法分配基本类型Integer
的值。
尝试替换它。
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