1

DBにReservation参照がないものをクエリで取得したい。エンティティにはエンティティがあります。単方向です。ReservationInstanceJPQLReservationInstanceReservationOneToOne

予約表

ID      NAME
0001    AAA
0002    BBB
0003    CCC
0004    DDD

予約インスタンス テーブル

ID      SOME_COLUMN     RESERVATION_ID  
0001    somedata        0004

Reservation参照のないリスト (AAA、BBB、および CCC の場合)を取得するにはどうすればよいReservationInstanceですか?

マイ エンティティ

public class Reservation implements Serializable {
    .....
    private String id;
    .....
    private String name;
    //getter and setter
}

public class ReservationInstance implements Serializable {
    ....
    private String id;
    ....
    private Date fromTime;
    ....
    private Date toTime;

    @OneToOne
    @JoinColumn(name = "RESERVATION_ID", referencedColumnName = "ID")
    private Reservation reservation;
}

投稿を更新

以下のようにクエリを使用しました

SELECT r FROM Reservation r LEFT JOIN ReservationInstance ri WHERE ri.reservation IS NULL

私は例外を取得します

[EL Severe]: 2012-12-18 16:37:02.817--ServerSession(1733641680)--Local Exception Stack: 
Exception [EclipseLink-8023] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [Reservation.findAbandonedReservation: SELECT r FROM Reservation r LEFT JOIN ReservationInstance ri WHERE ri.reservation.id IS
 NULL].
Internal Exception: org.eclipse.persistence.internal.libraries.antlr.runtime.EarlyExitException
    at org.eclipse.persistence.exceptions.JPQLException.syntaxError(JPQLException.java:352)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.handleRecognitionException(JPQLParser.java:350)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.addError(JPQLParser.java:242)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.reportError(JPQLParser.java:359)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.joinAssociationPathExpression(JPQLParser.java:11880)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.join(JPQLParser.java:11488)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.identificationVariableDeclaration(JPQLParser.java:11302)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.fromClause(JPQLParser.java:11164)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectStatement(JPQLParser.java:355)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.document(JPQLParser.java:275)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.parse(JPQLParser.java:130)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.buildParseTree(JPQLParser.java:91)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:207)
    at org.eclipse.persistence.internal.jpa.JPAQuery.processJPQLQuery(JPAQuery.java:106)
    at org.eclipse.persistence.internal.jpa.JPAQuery.prepare(JPAQuery.java:90)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:509)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:470)
    at org.eclipse.persistence.internal.sessions.AbstractSession.processJPAQueries(AbstractSession.java:1815)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:409)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:666)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:615)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:389)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:164)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:221)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:209)
    at org.ace.mrbs.ost.comp.reservation.persistence.ReservationDAO.deleteAbandonedReservation(ReservationDAO.java:110)
    at org.ace.mrbs.ost.comp.reservation.persistence.ReservationDAO.main(ReservationDAO.java:130)
Caused by: org.eclipse.persistence.internal.libraries.antlr.runtime.EarlyExitException
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.joinAssociationPathExpression(JPQLParser.java:11868)
    ... 23 more
4

1 に答える 1

3

私の最初の選択は、関係を双方向にすることです。

ON を使用した LEFT JOIN は JPA 2.1 でのみサポートされることが判明したため、次のようなことをお勧めします。

SELECT r FROM Reservation r WHERE r IS NOT IN (SELECT ri.reservation FROM ReservationInstance ri)

これが機能するかどうかは100%確信が持てません。そうでない場合r.id IS NOT INは、IDを返すクエリを使用してください。

于 2012-12-18T09:23:14.353 に答える