現在、短い例では、2 つのエンティティがPerson
ありApplicant
、データベース内のすべての応募者を取得するクエリを作成したい場合、次の設定で参照クエリを取得することになります。Single_Table_Inheritance 戦略を使用しています (fyi)
public class Person {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "schoolID")
private School school;
}
別のエンティティへの参照を持つ Teacher オブジェクト。
public class Applicant extends Person {
@OneToMany(targetEntity = ApplicantSchool.class, cascade = { javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.REMOVE }, mappedBy = "applicant")
private Set<ApplicantSchool> schools = new HashSet<ApplicantSchool>();
}
Person で学校関係をジャンプしてクエリしようとするクエリ試行
public List<Applicant> getAllApplicants(){
EntityManager entityManager = factory.createEntityManager();
@SuppressWarnings("unchecked")
List<Applicant> applicants = entityManager.createQuery("Select a from Applicant a LEFT JOIN FETCH a.schools WHERE a.active = :active ")
.setParameter("active", true).setHint("eclipselink.refresh", "true")
.getResultList();
//List<Applicant> applicants = Applicant.findAllApplicants();
System.out.println("The long query getting applicants");
entityManager.close();
return applicants;
}
ネストされた結合を使用するようにクエリを変更しようとすると、関係が認識されないというエラーが表示されます。私は次のようにクエリしようとしました:
entityManager.createQuery("Select a from Applicant a LEFT JOIN FETCH a.schools WHERE LEFT JOIN FETCH a.person.school a.active = :active ")
更新しました
キーワード「left join fetch」を削除した後、新しいクエリ:
List<Applicant> applicants = entityManager.createQuery("Select a from Applicant a WHERE a.active = :active ")
.setParameter("active", true)
.getResultList();
最初に作成することに同意する唯一のクエリはこれです
SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR FROM PERSON WHERE ((ACTIVE = ?) AND (TYPE = ?))
bind => [true, AP]
このスタック トレースを返し、次の SQL の選択が作成されます。
[EL Fine]: sql: 2012-07-13 17:25:52.297--ServerSession(1529073996)--Connection(726700617)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT schoolID, ACTIVE, ADMISSIONSEMAILADDRESS, ADMISSIONSPHONE, CODE, description, HELPGENERALEMAILADDRESS, NAME, PRIMARYPHONE, version, addressID FROM SCHOOL WHERE (schoolID = ?)
bind => [1]
[EL Fine]: sql: 2012-07-13 17:25:52.305--ServerSession(1529073996)--Connection(1615948530)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT id, ACTIVE, CODE, DESCRIPTION, NAME, version, SCHOOLDEPARTMENT_schoolID FROM DEPARTMENT WHERE (SCHOOLDEPARTMENT_schoolID = ?)
bind => [1]
[EL Fine]: sql: 2012-07-13 17:25:52.308--ServerSession(1529073996)--Connection(893810654)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT programID, ACTIVE, CODE, description, NAME, PROGRAMTYPE, REQUIREDCREDITS, version, SCHOOL_schoolID FROM PROGRAM WHERE (SCHOOL_schoolID = ?)
bind => [1]
[EL Fine]: sql: 2012-07-13 17:25:52.31--ServerSession(1529073996)--Connection(399107363)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR, studentId FROM PERSON WHERE (schoolID = ?)
bind => [1]
[EL Fine]: sql: 2012-07-13 17:25:52.314--ServerSession(1529073996)--Connection(464642021)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT registrationId, SEMESTERTYPE, version, YEAR, semesterCourseId, personID FROM REGISTRATION WHERE (personID = ?)
bind => [501]
[EL Fine]: sql: 2012-07-13 17:25:52.322--ServerSession(1529073996)--Connection(395757277)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT registrationId, SEMESTERTYPE, version, YEAR, semesterCourseId, personID FROM REGISTRATION WHERE (personID = ?)
bind => [552]
[EL Fine]: sql: 2012-07-13 17:25:52.327--ServerSession(1529073996)--Connection(328677993)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT registrationId, SEMESTERTYPE, version, YEAR, semesterCourseId, personID FROM REGISTRATION WHERE (personID = ?)
bind => [602]
[EL Fine]: sql: 2012-07-13 17:25:52.358--ServerSession(1529073996)--Connection(873312397)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT addressID, CITY, COUNTRY, STATE_US, STREETADDRESS, STREETADDRESS2, version, ZIPCODE FROM ADDRESS WHERE (addressID = ?)
bind => [2]
[EL Fine]: sql: 2012-07-13 17:25:52.365--ServerSession(1529073996)--Connection(509650638)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT applicantSchoolID, CREDITSCOMPLETED, FROMMONTH, FROMYEAR, GPA, NAME, SCHOOLTYPE, TOMONTH, TOYEAR, version, APPLICANT_personID FROM APPLICANTSCHOOL WHERE (APPLICANT_personID = ?)
bind => [151]
[EL Fine]: sql: 2012-07-13 17:25:52.385--ServerSession(1529073996)--Connection(17334753)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT applicantSchoolID, CREDITSCOMPLETED, FROMMONTH, FROMYEAR, GPA, NAME, SCHOOLTYPE, TOMONTH, TOYEAR, version, APPLICANT_personID FROM APPLICANTSCHOOL WHERE (APPLICANT_personID = ?)
bind => [51]