aspose file tools*
The moose likes Object Relational Mapping and the fly likes getting data from list returned through Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "getting data from list returned through Hibernate" Watch "getting data from list returned through Hibernate" New topic
Author

getting data from list returned through Hibernate

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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

Joined: Mar 18, 2011
Posts: 242

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

Joined: Jun 05, 2010
Posts: 618
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

Joined: Mar 18, 2011
Posts: 242

before typecast check for null may be this will work
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
before typecast check for null may be this will work


I have tried that. But its not null.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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

Joined: Oct 13, 2005
Posts: 36578
    
  16
mallikarjun dontamsetti wrote:before typecast check for null may be this will work
If you use instanceof, that is unnecessary.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Thanks Campbell Ritchie for your reply. What do you say about my previous post?
Rajit vreddi
Greenhorn

Joined: Jul 05, 2012
Posts: 17
first of all your query returns not a object type i.e TuitionPaymentEntity. you need to frame your query like this

Rajit vreddi
Greenhorn

Joined: Jul 05, 2012
Posts: 17
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

Joined: Jun 05, 2010
Posts: 618
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

Joined: Jul 05, 2012
Posts: 17
try this.it might work ..

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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

Joined: Jun 05, 2010
Posts: 618
No reply

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Kunal Lakhani wrote:No reply



Hi Kunal,

EntityManager is for JPA what session is for hibernate.

check out this example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting data from list returned through Hibernate
 
Similar Threads
How to get List elements
Could not parse mapping document from resource PojoTestTable.hbm.xml
Getting exception while passing List<Long> from hibernate to Stored Procedure
Executing stored procedures in hibernate
Hibernate: Column not found. org.hibernate.exception.GenericJDBCException