PropertyAccessException:访问字段时出错

时间:2017-01-13 20:27:45

标签: mysql spring hibernate spring-mvc spring-data-jpa

我有一个实体用户

@Entity
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;

    public Long getId() {
    return id;
}
...}

我正在尝试使用以下命令从数据库中删除用户:

@Modifying
@Query("DELETE FROM Collaborator c WHERE c.user = :userId AND c.task = :taskId")
void deleteUserFromTask(@Param("userId") Long userId, @Param("taskId") Long taskId);

这是User和Collaborator(我要删除的表)之间的关系:   在用户

@JsonIgnore
@OneToMany(mappedBy="user", fetch = FetchType.EAGER)
private Set<Collaborator> collaborators = new HashSet<>(); 

协作者

    @ManyToOne(cascade = {CascadeType.MERGE,
                      CascadeType.DETACH,
                      CascadeType.REFRESH }, 
           fetch = FetchType.EAGER)
@JoinColumn(name = "UserID", nullable = true)
private User user;

但是我收到了这个错误:

Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long gestionprojet.java.entities.beans.User.id] by reflection for persistent property [gestionprojet.java.entities.beans.User#id] : 1
  

Hibernate版本:5.2.6.FINAL

1 个答案:

答案 0 :(得分:2)

您的参数不匹配,即您尝试将Long指定为用户类型的参数:

您可以通过以下两种方式之一进行更改:

@Query("DELETE FROM Collaborator c WHERE c.user.id = :userId AND c.task.id = :taskId")
void deleteUserFromTask(@Param("userId") Long userId, @Param("taskId") Long taskId);

@Query("DELETE FROM Collaborator c WHERE c.user = :user AND c.task = :task")
void deleteUserFromTask(@Param("user") User user, @Param("task") Task task);