Reputation: 23226
On running the following snippet :
String hqlSelect = "select bookID from Issued where regID = " + regID;
List list = sess.createQuery(hqlSelect).list();
Iterator i = list.iterator();
while(i.hasNext()) {
Issued issued = (Issued) i.next(); // LINE : 46
bookIdList.add(issued.getBookID());
}
line number 46
generates an exception.
java.lang.ClassCastException: java.lang.Long cannot be cast to pojo.Issued
In the query bookID
and regID
are of type Long
and issued is a class that contains bookID
and regID
.
Why am I getting this exception ?
Upvotes: 1
Views: 195
Reputation: 513
try using below code.
String hqlSelect = "select new java.lang.Long(bookID) from Issued where regID = " + regID;
List<Long> list = (List<Long>)sess.createQuery(hqlSelect).list();
Iterator<Long> i = list.iterator();
while(i.hasNext()) {
bookIdList.add(i);
}
You can also use for each
loop
for (Long i : list) {
bookIdList.add(i);
}
Upvotes: 1
Reputation: 181
You do not need the select clause for selecting objects in HQL (see HQL Reference). So you can just use the following HQL query:
String hqlSelect = "from Issued where regID = " + regID;
and the query it will return a list of objects which can be cast to Issued.
Upvotes: 0
Reputation: 21961
Use following code to get bookID
String hqlSelect = "select bookID from Issued where regID = " + regID;
List<Object[]> list = sess.createQuery(hqlSelect).list();
for (Object[] result : list) {
long bookId = ((Number) result[0]).longValue();
}
Upvotes: 0
Reputation: 329
String hqlSelect = "select bookID from Issued where regID = " + regID;
This query seems like a normal native query. It will give Object[] List.
List<Object> list = sess.createNativeQuery(hqlSelect).list();
if u are using JPA query you can get Issued objects (if you have POJO Object for Issued)
Upvotes: 0