使用MEMBER OF与由复合键标识的实体集合

时间:2012-04-17 16:21:19

标签: java jpa persistence jpql

我正在尝试返回一组不属于这样的集合的对象:

    TypedQuery<Extra> q = em
            .createQuery(
                    "SELECT e FROM Extra e, Arrangement a WHERE "
                        + "a.id = :arrid AND e NOT MEMBER OF a.extras",
                    Extra.class);
    q.setParameter("arrid", a.getId());
    return q.getResultList();

但是我得到了一个错误的SQL:

Caused by: org.h2.jdbc.JdbcSQLException: Subquery is not a single column query; SQL statement:
select extra0_.code as code427_, extra0_.edition_number as edition5_427_,
extra0_.totalCost as totalCost427_, extra0_.unitCost as unitCost427_,
extra0_.units as units427_ from extra_arrangements extra0_
cross join arrangements arrangemen1_
where arrangemen1_.id=? and
((extra0_.code, extra0_.edition_number) not in
  (select extra3_.code, extra3_.edition_number
   from arrangements_extra_arrangements extras2_, extra_arrangements extra3_
   where arrangemen1_.id=extras2_.arrangements_id and
   extras2_.extras_code=extra3_.code and
   extras2_.extras_edition_number=extra3_.edition_number)) [90052-161]

问题是这个其他查询按预期工作:

    TypedQuery<Exhibitor> q = em.createQuery(
        "SELECT e FROM Exhibitor e, Edition ed WHERE ed.number = :ednum AND "
        + "e NOT MEMBER OF ed.exhibitors",
        Exhibitor.class);
    q.setParameter("ednum", e.getNumber());
    return q.getResultList();

唯一的区别是参展商有一个列ID,而Extra有一个复合键:

@Entity
@Table(name = "exhibitors")
public class Exhibitor implements Serializable, Comparable<Exhibitor> {

    @Id
    private int id;
    @Column(length = 100)
    private String code;
    @ManyToMany
    private List<Edition> edition;
    ...
}

额外:

@Entity
@Table(name="extra_arrangements")

public class Extra implements Serializable {

    @EmbeddedId
    private ExtraCode id;
    private double unitCost;
    private double units;
    ...
}


@Embeddable
public class ExtraCode implements Serializable {

    private String code;
    @ManyToOne
    private Edition edition;

    //Getters and setters omitted
}

知道我可能做错了什么以及如何解决它?

1 个答案:

答案 0 :(得分:0)

我认为这与this one的问题相同 H2 does not currently support that style of IN query.

sql-query中有问题的地方:

  AND (
    (
      extra0_.code,
      extra0_.edition_number
    ) NOT IN 
    (SELECT ...)
  )