我无法弄清楚如何为多对多映射编写 jpql 查询。
我有一个申诉实体,它与类别实体具有多对多关系。
public class Grievance {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(columnDefinition = "CHAR(36)")
@JdbcTypeCode(SqlTypes.VARCHAR)
private UUID id;
@Column(nullable = false)
private String title;
@Enumerated(EnumType.STRING)
private GrievanceStatus grievanceStatus;
@Column(nullable = false)
private String askedBy;
@CreationTimestamp
private LocalDateTime asked;
@UpdateTimestamp
private LocalDateTime modified;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_usn")
private Student student;
@ManyToMany
@JoinTable(
name = "grievance_category",
joinColumns = @JoinColumn(name = "grievance_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "category_name", referencedColumnName = "category"))
private List<Category> categories;
...other relations
}
为了显示学生提出的所有申诉,我将 StudentGrievances 发送至 dto
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StudentGrievances {
private UUID id;
private String title;
private GrievanceStatus grievanceStatus;
private String askedBy;
private LocalDateTime asked;
private LocalDateTime modified;
private long timeTaken;
private List<Category> categories;
}
我在学生存储库中编写了一个查询
@Query("SELECT new com.rnsit.studentgrievance.dto.student.StudentGrievances(g.id, g.title, g.grievanceStatus, g.askedBy, g.asked, g.modified, g.timeTaken, g.categories) FROM Grievance g JOIN g.categories c WHERE g.student.usn = :usn")
List<StudentGrievances> getGrievancesByUsn(String usn);
我收到以下错误
Cannot instantiate class 'com.rnsit.studentgrievance.dto.student.StudentGrievances' (it has no constructor with signature [java.util.UUID, java.lang.String, com.rnsit.studentgrievance.entity.grievance.GrievanceStatus, java.lang.String, java.time.LocalDateTime, java.time.LocalDateTime, java.lang.Long, java.lang.String, java.lang.String], and not every argument has an alias)
您的所有 Args Lombok 注释可能会创建一个接受 Collection 作为最后一个参数的构造函数,而 CriteriaApi 需要 2 个字符串。如果删除注释并使用接受变量 args String... 类别的手动构造函数,那么它应该可以工作