Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Native query. How to get the data...

843830Dec 3 2007
Hello everyone!

I'm struggling to figure out how to extract data from particular columns from DB. Here is what I have...

A table PersData with columns A, B, C, D and so forth. Also there is an entity represents this table called PersonalData.

I need to get the data from A and C columns, but I still haven't found a solution how to do this. I know how to exctract an entire row from the PersData table by writing the following code...
Query query = entityManager.createNativeQuery("SELECT * FROM 'PersData` ", PersonalData.class);
							
List<PersonalData> qresult = query.getResultList();

for(PersonalData item: qresult){
      System.out.println(item.getA());
      .....
... But it's inefficient to get values of other columns at the same time.

I guess that my query should be written smth like this...
Query query = entityManager.createNativeQuery("SELECT A, C FROM `PersData` ");
List<PersonalData> qresult = query.getResultList();

for(PersonalData item: qresult){
      System.out.println(item.getA());
      .....
But I got an error java.lang.ClassCastException when I do this for (PersonalData item: qresult). Actually, I understand why it's like this.

Does anybody know how to get values just from two columns and then use them?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2007
Added on Dec 3 2007
0 comments
179 views