没有CriteriaBuilder的jpql对象IN列表

时间:2013-11-22 10:02:44

标签: java-ee jpa eclipselink criteria jpql

我必须将所有Invoice Invoice.responsible放在Usergroup的列表中?

我试过以下:

public List<Invoice> getOldestInvoiceWithOpenBenefits(List<Usergroup> usergroups) {

    TypedQuery<Invoice> q = em.createQuery("SELECT i FROM Invoice i WHERE (i.responsible IN (:usergroups)) AND "
            + " i.entered = false AND i.totalcosts > (SELECT SUM(b.costs) FROM Benefit b WHERE b.invoice = i) ORDER BY i.creationdate ASC",
            Invoice.class);
    q.setParameter("usergroups", usergroups);
    q.setMaxResults(10);
    return q.getResultList();
}

但是我得到了这个错误:

Information: /pages/index.xhtml
Warnung: EJB5184:A system exception occurred during an invocation on EJB InvoicEJB, method: public java.util.List de.ltg.sachzuwendungen.ejb.InvoicEJB.getOldestInvoiceWithOpenBenefits(java.util.List)
Warnung: javax.ejb.EJBException
    [...Stacktrace]
Caused by: Exception [EclipseLink-6075] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: Object comparisons can only use the equal() or notEqual() operators.  Other comparisons must be done through query keys or direct attribute level comparisons. 
Expression: [
Relation operator [ IN ]
   Query Key responsible
      Base de.ltg.sachzuwendungen.db.entity.Invoice
   Constant [
Parameter usergroups]]
    at org.eclipse.persistence.exceptions.QueryException.invalidOperatorForObjectComparison(QueryException.java:646)
[...Stacktrace]

有人可以在不使用CriteriaBuilder的情况下帮助我完成这项工作吗? 如果您需要数据库实体,请询问。

提前谢谢。

修改

发票实体:

@Entity
@Table(name = "t_invoice")
public class Invoice implements Serializable {

    private static final long serialVersionUID = 1L;

    @Version
    @Column(name = "version")
    /**
     * Optimistic Locking
     */
    private int version;
    @Id
    @Column(name = "id")
    private int id;
    @Column(name = "number")
    private String number;
    @Column(name = "name")
    private String name;
    @Column(name = "description")
    private String description;
    @Column(name = "totalcosts")
    private double totalcosts;
    @Temporal(TemporalType.DATE)
    @Column(name = "issuedate")
    private Date issuedate;
    @Temporal(TemporalType.DATE)
    @Column(name = "creationdate")
    private Date creationdate;
    @Column(name = "entered")
    private boolean entered;
    @JoinColumn(name = "idUser_creator", referencedColumnName = "id")
    private User creator;
    @JoinColumn(name = "idUsergroup_responsible", referencedColumnName = "id")
    private Usergroup responsible;
    @OneToMany(mappedBy = "invoice")
    private List<Benefit> benefits;

    public Invoice() {
        // TODO Auto-generated constructor stub
    }
/*Getter & Setter*/
..
/*Override toString()*/
..
}

用户组实体:

  

@Entity
@Table(name = "t_usergroup")
public class Usergroup implements Serializable {

    private static final long serialVersionUID = 1L;

    @Version
    @Column(name = "version")
    /**
     * Optimistic Locking
     */
    private int version;

    @Id
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;

    @ManyToMany(targetEntity = Right.class, fetch = FetchType.LAZY)
    @JoinTable(name = "t_mapUsergroupToRight", joinColumns = { @JoinColumn(name = "idUsergroup") }, inverseJoinColumns = { @JoinColumn(name = "idRight") })
    private List<Right> rights;

    @ManyToMany(targetEntity = User.class, fetch = FetchType.LAZY)
    @JoinTable(name = "t_mapUserToUsergroup", joinColumns = { @JoinColumn(name = "idUsergroup") }, inverseJoinColumns = { @JoinColumn(name = "idUser") })
    private List<User> users;

    /*Getter & Setter*/
    ..
    /*Override toString() & equals() & hashCode()*/
    ..

   }
  

1 个答案:

答案 0 :(得分:0)

您的问题在i.responsible IN (:usergroups)。它应该是i.responsible IN :usergroups(没有括号)。还要记住,一些JPA实现抛出异常,列表是空的,所以你应该介意检查它。