Reputation:
I need to get a single cell value from the table. And I am using hibernate with Spring MVC.
I did is:
Session s = getSessionFactory().getCurrentSession();
query q= s.createQuery("select xxx from abc where asd=:asd");
now, I need to save this single value into a string variable and need to use this string variable to compare with other string in a jsp page using <c:choose> <c:when> <c:otherwise>
tags...
This is what I have now..
@Transactional
public String getUseNis(String code) {
Session s = getSessionFactory().getCurrentSession();
Query q = s.createQuery("select useNis from tle_location where code = :code");
q.setString("code", code);
String result = (String) q.uniqueResult();
return result;
I need to use the return value in a jsp page to check for either yes or no...how to pass it to my jsp page..
Upvotes: 2
Views: 10618
Reputation: 19027
To get a single result, use either getFirstResult()
or getSingleResult()
. Note that neither is particularly useful in your case for these reasons:
getFirstResult()
will return the first element in the
collection returned by your query which may or may not be the
element you are looking for.getUniqueResult()
expects the query to return
exactly one item, and causes an exception to be thrown, if the collection returned is empty or has more than one entry.An illustration with uniqueResult()
.
//sessionFactory = getHibernateTemplate().getSessionFactory();
Session session = getSessionFactory().getCurrentSession();
Query query = session.createQuery("select value from table where ...");
query.setParameters("param1", value1);
result = (Type) query.uniqueResult(); //The type is you desired result type.
//test for null here if needed
Make sure that the query returns only a single result. It will throw NonUniqueResultException
- if there more than one matching result is found (Therefore query.setMaxResults(1);
would be more appropriate).
http://www.java2s.com/Code/Java/Hibernate/UniqueResultHQL.htm
Upvotes: 5
Reputation: 8971
In Controller write below code
// Fetch the result by executing query and store it into string variable as
String typeofpayment = serviceObj.getValueFromDB();
model.addAttribute("typeofpayment",typeofpayment); // This will pass attribute to jsp page
Now in jsp you can use
<c:set var="typeOfPaymentVar" value="${typeofpayment}" scope="request" />
<c:choose>
<c:when test='${typeOfPaymentVar=="cash"}'>
CASH
</c:when>
<c:otherwise>
OTHER MODE OF PAYMENT
</c:otherwise>
</c:choose>
Upvotes: 0
Reputation: 42074
First you have have to set value for :asd, but that is probably clear.
Then you go and execute query and convert result. Because you seem to expect that this query returns single value, it can be executed via Query.uniqueResult. If you want to take some specific action in case when assumption about one and only one non-null result was wrong, please react to returned null value or thrown NonUniqueResultException accordingly.
How the result of query is converted to string depends of course about type of xxx
and preferred format of String presentation. Some examples follows:
//type of xxx is already string
String result = (String) q.uniqueResult();
//type of xxx is Integer
Integer res = (Integer) q.uniqueResult();
String result = res.toString();
Upvotes: 0