我正在使用JPA查询来获取结果集,然后在同一个类中,我想有条件地获取更多数据。这是它的样子:
public SchoolUser getCandidatesAsJson(@PathParam("applicationId") String applicationId, @PathParam("userPassword") String userPassword ) {
EntityManager em = createEM();
Query query = em.createQuery("SELECT su FROM SchoolUser su WHERE su.applicationId LIKE :applicationId and su.userPassword LIKE :userPassword", SchoolUser.class);
query.setParameter("applicationId", applicationId);
query.setParameter("userPassword", userPassword);
List <SchoolUser> schoolUser = query.getResultList();
if(!schoolUser.isEmpty()) {
SchoolUser loginRecord = schoolUser.get(0);
int teacherId = loginRecord.getTeacherId();
int studentId = loginRecord.getStundentId();
if(teacherId!=0){
TypedQuery<Classroom> query2 = em.createQuery("SELECT c FROM Classroom c where c.teacherId = :teacherId ORDER BY c.period", Classroom.class);
query2.setParameter("teacherId", teacherId);
List <Classroom> teacherClassList = query2.getResultList();
if(!teacherClassList.isEmpty()){
//put 2nd results set in SchoolUser object - line is commented because it causes an erro
//loginRecord.setClassRooms(teacherClassList);
}
} else if(studentId!=0){
TypedQuery<ClassroomStudent> query3 = em.createQuery("SELECT cs FROM ClassroomStudent cs where cs.statusId = 1 AND cs.studentId = :studentId", ClassroomStudent.class);
query3.setParameter("studentId", studentId);
//put results in SchoolUser object
}
return loginRecord;
} else {
SchoolUser emptyRecord = new SchoolUser();
return emptyRecord;
}
}
错误来自将Classroom JPA对象放入SchoolUser对象 - 因为这两个对象没有直接关系。
我可以通过JPA以任何方式完成此任务吗?
答案 0 :(得分:1)
如果您不想保留classroom
(或该问题的任何其他属性),则@Transient
注释允许您忽略特定字段,以便JPA
不会尝试映射它。
此注释指定属性或字段不是 执着。它用于注释实体的属性或字段 class,mapping superclass或embeddable class。
Example:
@Entity
public class Employee {
@Id int id;
@Transient User currentUser;
...
}