如何在HQL查询中获取唯一记录

时间:2014-05-17 12:44:04

标签: sql oracle hibernate

我有两个表TUser和TComment,关系1到多个。我想写一个Hibernate查询,它提取唯一的TUsers,它们做了评论。

我试过了:

EntityManager em = HibernateUtil.getEntityManager();

        Query queryCommentsAuthors = em.createQuery("select  u.author  from TComment u group by u.author");

错误:

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLException: ORA-00979: not a GROUP BY expression

和此:

Query queryCommentsAuthors = em.createQuery("select distinct u.author from TComment");

错误:

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLException: ORA-00932: inconsistent datatypes: expected - got BLOB

但没有成功。我很感激任何处理这个问题的建议。

这是我的bean类的一部分,我有TUser对象:

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

    private TUser author;
    ....
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "AUTHOR_ID", nullable = false)
    public TUser getAuthor() {
        return this.author;
    }

    public void setAuthor(TUser author) {
        this.author = author;
    }
    .....
}

这是表格的DDL:

create table T_COMMENT
(
  id           NUMBER(19) not null,
  comment_body VARCHAR2(4000 CHAR) not null,
  comment_date DATE not null,
  author_id    NUMBER(19) not null,
)

0 个答案:

没有答案