Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting data from list returned through Hibernate

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. This is my hibernate code for getting details from database as per enrollment number



The sql query contains table name as 'tuitionpayment' which is matched to TuitionPaymentEntity in the hbm.xml

(I have used TuitionPaymentEntity to the list in the code). The returned list contains fullname(i.e string),payment(int),due(int) which is also the property of TuitionPaymentEntity class. How can i get those data?

This is what i tried :



But this code gives this exception

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.nit.Entity.TuitionPaymentEntity (In the line number 264)

How can i get those data?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, this Question is similar to doubt asked in another forum, but here i have included hibernate details. And i don't think that the doubt asked earlier is different from this.


Sorry Once again for duplicating the doubt in two different forums.

But, still my doubts in both forum are unanswered.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
TuitionPaymentEntity tp = (TuitionPaymentEntity)lastPaymentDetails.get(0);

Which will give you first object. because list object contains list of TuitionPaymentEntity objects so..
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply mallikarjun dontamsetti.

That gives the error :

[Ljava.lang.Object; cannot be cast to org.nit.Entity.TuitionPaymentEntity
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before typecast check for null may be this will work
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before typecast check for null may be this will work


I have tried that. But its not null.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works




But is it a good way of coding? This works because i know the order in which columns are retrieved. But what if my sql query was something like "SELECT * from....."
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun dontamsetti wrote:before typecast check for null may be this will work
If you use instanceof, that is unnecessary.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie for your reply. What do you say about my previous post?
 
Rajit vreddi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all your query returns not a object type i.e TuitionPaymentEntity. you need to frame your query like this

 
Rajit vreddi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or
if the query is not a hql then use native query.the native query return object array so the code should be like this..
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Rajit vreddi.


if the query is not a hql then use native query.the native query return object array so the code should be like this..
view plaincopy to clipboardprint?
List<Object[]> lastPaymentDetails = query.list();
for(Object[] obj:lastPaymentDetails ){

String abc = (String)obj[0];
Integer xyz = (Integer)obj[1];
..
use corresponding datatypes sql type to java type


}


This is what i have used. But what if i have to retrieve 10 columns. Will i have to follow the same step?
 
Rajit vreddi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this.it might work ..

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply

But, i barely have any idea about EntityManager in Hibernate. (Moreover i have seen people using EntityManager with annotations, but still i don't know anything).

There would be any other solution in java. (I am keeping Hibernate aside for some time)
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No reply

 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:No reply



Hi Kunal,

EntityManager is for JPA what session is for hibernate.

check out this example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic