query.count() throws exception with Embeddable ID

时间:2015-07-28 17:09:55

标签: java hibernate jpa

I'm not able to get the total count of my query.

JPAQuery query = super.prepareJPAQuery(userAccountHasWorkgroup).where(
    userAccountHasWorkgroup.workgroup.id.eq(workgroupId);
query.count();

userAccountHasWorkgrouphas an @Embeddable class as ID.

javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.DataException: Operand should contain 1 column(s)

I add more information:

@Entity
@Table(name = "UserAccount_has_Workgroup")
public class UserAccountHasWorkgroup implements java.io.Serializable {

    private static final long serialVersionUID = 6537213525312531347L;

    private UserAccountHasWorkgroupId id;

    private UserAccount userAccountByUserAccountId;
    private Privilege privilege;
    private UserAccount userAccountByApprovedByUserAccountId;
    private Workgroup workgroup;
    private boolean approved;
    private boolean lastActiveWorkgroup;
    private boolean isWorkgroupReferent;

    ...
}

@Embeddable
public class UserAccountHasWorkgroupId implements java.io.Serializable {

    private static final long serialVersionUID = 6368469866573301127L;
    private long userAccountId;
    private long workgroupId;

    ...
}

If i do:

List<UserAccountHasWorkgroup> result = query.list(userAccountHasWorkgroup);

it works, but when I try to count:

Long count = query.count();

I receive the DataException

1 个答案:

答案 0 :(得分:0)

而不是:

query.count();

我做

query.uniqueResult(Wildcard.count.as("count"));

它似乎运作正常。