我正在尝试使用下面的JPQL对Faculty
实体进行排序,但我检索的ID数量太多了。
@NamedQuery(
name = "Faculty.findFacultySortedByPreferredTime",
query = "SELECT f FROM Faculty f JOIN f.preferredTimes p ORDER BY p.day, p.startTime"
),
这是我正在工作的Faculty
实体
public class Faculty implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String middleName;
private String lastName;
@OneToMany(cascade = CascadeType.PERSIST)
@OrderBy(value = "day, startTime")
private List<PreferredTime> preferredTimes;
// some codes removed
我做错了吗?
答案 0 :(得分:1)
在大多数情况下,JPA会为您从数据库中选择的每一行返回一个实体,因此当加入1:M或M:M关系时,您将获得一个以上的对象实例。在这种情况下,Faculty有许多首选时间。如果faculty1具有preferredTimeA和preferredTimeC而faculty2具有preferredTimeB和preferredTimeD,则由于您指定的连接和排序,您将获得(faculty1,faculty2,faculty1,faculty2)的有序列表。
DISTINCT关键字用于过滤掉不同的实体/行:
"SELECT DISTINCT f FROM Faculty f JOIN f.preferredTimes p ORDER BY p.day, p.startTime"