这个JPA COUNT select查询有什么问题吗?

时间:2022-05-18 00:07:57

In my db, I have a table (Defaults), and when I generate an entity from table, I get these two classes:

在我的db中,我有一个表(默认值),当我从表生成一个实体时,我得到这两个类:

@Entity
public class Defaults implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected DefaultsPK DefaultsPK;
    @Column(name = "ERTEK")
    private String ertek;

    getter/setter...
}

@Embeddable
public class DefaultsPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "VALUE_1")
    private String value1;
    @Basic(optional = false)
    @Column(name = "TYPE")
    private String type;
    @Basic(optional = false)
    @Column(name = "VALID_FROM")
    @Temporal(TemporalType.TIMESTAMP)
    private Date validFrom;
    @Basic(optional = false)
    @Column(name = "VALID_TO")
    @Temporal(TemporalType.TIMESTAMP)
    private Date validTo;

    getter/setter...
}

That is why becaues the primary key is including the values. I want to count all the rows in the table, so I use this code:

这就是为什么becaues的主键包括值。我想要数表中的所有行,所以我使用了以下代码:

String sql = "SELECT COUNT(d) FROM Defaults d";
Query q = em.createQuery(sql);
long count = (long)q.getSingleResult();

But I am getting this error:

但是我得到了这个错误:

org.hibernate.exception.SQLGrammarException: could not execute query
...
java.sql.SQLSyntaxErrorException: ORA-00907: The right expression is missing from the arithmetic expression

What is the problem? The other count queries with other entities are working.

这个问题是什么?与其他实体的其他计数查询正在工作。

I am using hibernate.

我使用hibernate。

1 个解决方案

#1


44  

Use count(d.ertek) or count(d.id) instead of count(d). This can be happen when you have composite primary key at your entity.

使用count(d.ertek)或count(d.id)代替count(d)。当您的实体中有复合主键时,就会发生这种情况。

#1


44  

Use count(d.ertek) or count(d.id) instead of count(d). This can be happen when you have composite primary key at your entity.

使用count(d.ertek)或count(d.id)代替count(d)。当您的实体中有复合主键时,就会发生这种情况。