18
在我的數據庫,我有一個表(默認值),而當我生成表的實體,我得到這兩個類:此JPA COUNT選擇查詢有什麼問題?
@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...
}
這就是爲什麼becaues主鍵包括的值。 我想算表中的所有行,所以我用這個代碼:
String sql = "SELECT COUNT(d) FROM Defaults d";
Query q = em.createQuery(sql);
long count = (long)q.getSingleResult();
但我收到此錯誤:
org.hibernate.exception.SQLGrammarException: could not execute query
...
java.sql.SQLSyntaxErrorException: ORA-00907: The right expression is missing from the arithmetic expression
問題是什麼?與其他實體的其他計數查詢正在工作。
我正在使用hibernate。
是的,這是工作:'SELECT COUNT(d.DefaultsPK.value1)FROM Defaults d' – victorio