简体   繁体   中英

Can I specify the result object type for an HQL query?

I have an entity in a hibernate instance called keywords. It stores a list of words and the number of times each word occurs. Words are not unique. I want to sum the total for each word in the list. I use the HQL

query select keyword, sum(keywordcount) from Keywords as Keywords group by keyword order by sum(keywordcount) desc

Which gives me the right result. The issue I have is that when I submit this query I get a list of java.lang.Object objects. Is there a way I can tell HQL to give me a list of objects of type Keywords, since these have the structure I want.

@Entity
@Table(name = "keywords", catalog = "akiradev")
public class Keywords implements java.io.Serializable {

// Fields

private Integer id;
private Documents documents;
private String keyword;
private Integer keywordcount;

// Constructors

/** default constructor */
public Keywords() {
}

/** full constructor */
public Keywords(Documents documents, String keyword, Integer keywordcount) {
    this.documents = documents;
    this.keyword = keyword;
    this.keywordcount = keywordcount;
}

// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "document_id")
public Documents getDocuments() {
    return this.documents;
}

public void setDocuments(Documents documents) {
    this.documents = documents;
}

@Column(name = "keyword")
public String getKeyword() {
    return this.keyword;
}

public void setKeyword(String keyword) {
    this.keyword = keyword;
}

@Column(name = "keywordcount")
public Integer getKeywordcount() {
    return this.keywordcount;
}

public void setKeywordcount(Integer keywordcount) {
    this.keywordcount = keywordcount;
}
}

------- query --------

public List<Keywords> getKeywordSum() {
    try {

        String queryString = "select keyword, sum(keywordcount) from Keywords as Keywords group by keyword order by sum(keywordcount) desc";
        Query queryObject = getSession().createQuery(queryString);
        List<Keywords> results = (List<Keywords>) queryObject.list();
        return results;
    } catch (RuntimeException re) {
        log.error("finding Documents in descending time order failed", re);
        throw re;
    }
}

Using Keyword as a return type here doesn't make much sense because query results don't have the same identity as keywords. However, you can create a DTO to represent results of this query

public class KeywordStats {
    private String keyword;
    private int count;
    public KeywordStats(String keyword, int count) { ... }
    ...
}

and use constructor syntax to return it from the query:

select new KeywordStats(keyword, sum(keywordcount)) from Keywords as Keywords group by keyword order by sum(keywordcount) desc  

Actually, you can use the same approach to return Keyword s, but I wouldn't recommend it since it's an abuse of entity object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM