Anusha Bayya
Anusha Bayya

Reputation: 103

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

Upvotes: 0

Views: 1809

Answers (1)

Markus Fischer
Markus Fischer

Reputation: 1366

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 (i.e. namein 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).

Upvotes: 1

Related Questions