我有一个实体用户:

@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);

这是用户和协作者(我要从中删除的表)之间的关系:
中用户
@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

休眠版本:5.2.6.FINAL

最佳答案

您的参数不匹配,即您尝试为用户类型指定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);

关于mysql - PropertyAccessException:访问字段时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41642939/

10-08 20:42