1

我正在使用 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 完成这个吗?

4

1 回答 1

1

如果您不想保留classroom(或任何其他属性),那么@Transient注释允许您忽略特定字段,这样JPA就不会尝试映射它。

此注释指定属性或字段不是持久的。它用于注释实体类、映射超类或可嵌入类的属性或字段。

Example:
@Entity
public class Employee {
    @Id int id;
    @Transient User currentUser;
    ...
}
于 2013-10-27T02:31:53.283 回答