JPQL中的IN语句与GAE

时间:2013-04-19 20:10:28

标签: google-app-engine jpa jpql

我尝试将JPA与GAE一起使用,我有下一个问题:

执行此查询时:

select x From Advert x where x.advertType in (15)

select x From Advert x where x.advertType in (20)

实体管理器在两种情况下都返回一个实体 但是当我尝试执行时

select x From Advert x where x.advertType in (15,20)

重新生成零实体

这是我的映射:

@Entity
public class Advert extends DomainObject implements Comparable<Advert> {
    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @Unowned
    private AdvertType advertType;
    ...
}

@Entity
public class AdvertType extends DomainObject implements Comparable<AdvertType> {
    private Integer position;
    private String typeId;
    private String titleMain;
}

@Entity
@MappedSuperclass
public abstract class DomainObject implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;
}

日志:

2013-04-20 12:48:17 DEBUG Query:58 - QueryCompilation:
  [from:ClassExpression(alias=x)]
  [filter:DyadicExpression{DyadicExpression{PrimaryExpression{x.advertType}  =  Literal{15}}  OR  DyadicExpression{PrimaryExpression{x.advertType}  =  Literal{20}}}]
  [symbols: x type=net.org.selector.animals.domain.Advert]
2013-04-20 12:48:17 DEBUG Query:58 - JPQL Query : Executing "SELECT x FROM Advert x WHERE x.advertType in (15,20)" ...
2013-04-20 12:48:17 DEBUG Query:58 - Query compiled as : Kind=net.org.selector.animals.domain.Advert Filter : advertType_key=AdvertType(15) AND advertType_key=AdvertType(20) [QUERY-TYPE=NORMAL]
2013-04-20 12:48:17 DEBUG Native:58 - Executing query in datastore for SELECT x FROM Advert x WHERE x.advertType in (15,20)

那么为什么查询编译为:

advertType_key = AdvertType(15) AND advertType_key = AdvertType(20)

而不是

advertType_key = AdvertType(15) OR advertType_key = AdvertType(20)

1 个答案:

答案 0 :(得分:0)

您的查询存在缺陷。 “x.advertType”的类型为AdvertType,因此它不能“在”数字列表中。如果它位于字符串列表中,则可以使用“x.advertType.typeId”。此外,日志还会告诉您实际调用的低级查询