1

I'm writing some DAO objects for my application, which communicate with oracle database (object relational database). Everything works fine, except for this part of the code:

List<Object>grades = new ArrayList<>();

Array gradesList = (Array)rs.getArray("GRADES");
grades = Arrays.asList(gradesList.getArray());

With this i get List<Object>. What i really need is List<Grade>. I searched the internet for answer but I could not find any. I tried to cast objects from grades list in for loop and add them to another List but that didn't work. Could it be done somehow?

2
  • "I tried to cast objects from grades list in for loop and add them to another List but that didn't work". What didn't work about this? Did you get an exception? Did something else happen? Please edit your question to include the details of why this doesn't work for you. Commented Feb 2, 2018 at 21:32
  • @LukeWoodward I have found the solution. Regarding "I tried to cast objects from grades list in for loop and add them to another List but that didn't work" i think I got [Ljava.lang.Object; cannot be cast to data.models.Grade exception Commented Feb 2, 2018 at 23:46

3 Answers 3

2

You can cast list to another list by first casting it to a generic list then to List of Grade like this;

List<Grade> gradeList = (List<Grade)(List<?>) grades;

Sign up to request clarification or add additional context in comments.

Comments

1

I have found the solution for my problem. Since I had used oracle database within my application, I used these packages:

import oracle.sql.ARRAY;
import oracle.sql.Datum;
import oracle.sql.STRUCT;  

I need to mention that "GRADES" field in one of my database tables was organized as nested table. Then I got my List of Grade objects like this:

List<Grade>grades = new ArrayList<>();
ARRAY gradesList = (ARRAY)rs.getObject("GRADES");
if(gradesList != null && gradesList.length() != 0) {
    Datum[] elems = gradesList.getOracleArray();    

    for(int i = 0; i<elems.length;i++) {
        STRUCT gradeInfo = (STRUCT) elems[i];
        Object[] gradeInfoValues = gradeInfo.getAttributes();
        Grade grade = new Grade();
        /*SETTING GRADE OBJECT ATTRIBUTES BY USING gradeInfoValues ARRAY ...*/
        grades.add(grade);
        }
    }   

And that was it!

https://docs.oracle.com/cd/A97630_01/java.920/a96654/oraarr.htm

Comments

0

If you are using Java 8 and assuming you have List<Object>, which needs to be converted to List<Grade>. Then, you can make use of stream as followed.

final List<Grade> gradeList = grade.stream()
                             .filter(Grade.class::isInstance)
                             .map(Grade.class::cast)
                             .collect(toList());

filter() to exclude items that will cause type casting exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.