简体   繁体   中英

Return a string using HQL

*

Query query = session.createQuery("SELECT c.name FROM CompanyEntity c WHERE c.id = :companyId");
query.setInteger("companyId", companyId);
result = query.toString();

Hi.I am fetching name which is String from query. But the result is not returned properly.I am getting the query as a result.Please help.

Thanks

The Query object capsulates the formulated query itself, not the result of the query. In order to execute the query and retrieve the result, you have to call

query.list();

which returns a List of the selected properties (ie name in this case).

If your query returns a single result, there is a convenience method:

query.uniqueResult();

And if your query is an update statement, you can execute it without having any result:

query.executeUpdate();

(this last one returns the number of updated entities).

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