我正在做一些学术任务而且我被困住了。情况如下:
我有三个简单的数据库表和从它们生成的Java @Entity类。
表标记:
STUDENT_ID(PK)
SUBJECT_ID(PK)
MARK
public class Mark implements Serializable {
private BigDecimal mark;
@Id
@Column(name = "STUDENT_ID", nullable = false, insertable = false, updatable = false)
private BigDecimal studentId;
@Id
@Column(name = "SUBJECT_ID", nullable = false, insertable = false, updatable = false)
private BigDecimal subjectId;
@ManyToOne
@JoinColumn(name = "SUBJECT_ID")
private Subject subject;
@ManyToOne
@JoinColumn(name = "STUDENT_ID")
private Student student;
(...)
}
表学生:
INDEX_NO(PK)
FNAME
LNAME
FACULTY_ID
public class Student implements Serializable {
@Column(name = "FACULTY_ID", nullable = false)
private BigDecimal facultyId;
@Column(nullable = false, length = 20)
private String fname;
@Id
@Column(name = "INDEX_NO", nullable = false)
private BigDecimal indexNo;
@Column(nullable = false, length = 20)
private String lname;
@OneToMany(mappedBy = "student")
private List<Mark> marksList1;
(...)
}
表学生:
ID(PK)
NAME
public class Subject implements Serializable {
@Id
@Column(nullable = false)
private BigDecimal id;
@Column(nullable = false, length = 60)
private String name;
@OneToMany(mappedBy = "subject")
private List<Mark> marksList;
(...)
}
任务是获得两个清单:
1.尚未获得所有科目分数的学生名单
2.特定学生没有标记的科目列表。
我不太擅长SQL,所以我的查询构造有问题。它可以通过任何类似JPA的方式解决,即JPQL,Criteria API。
所以我请你帮忙 感谢您的关注。
答案 0 :(得分:1)
我不确定这是否正确但也许这可以帮助您解决问题。 JPQL中的size()函数可以对集合进行计数。
这是2个JPQL查询
1)
select m.student
from Mark m
where size(m.subject) <>
size(select s from Subject s)
2)
select s
from Subject s
where s not in
(select m.s from Mark m where m.student.indexNo = :indexNo)
:indexNo则是应该使用参数设置的学生ID。