PropertyAccessException:访问字段时出错

我有一个实体 :

@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

hibernate版本: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);

以上是 PropertyAccessException:访问字段时出错 的全部内容, 来源链接: utcz.com/qa/431274.html

回到顶部