2

I have a function that returns an Object

The toString() method shows that my object has two BigDecimal attributes. But I don't know how to get them in the code ?

enter image description here

My function uses hibernate to get results from a query is :

public Object executeQuery(final String sql) { final Query query = getSessionFactory().getCurrentSession().createSQLQuery(sql); return query.list().get(0); } 

Thank you.

-- Additional infos:

obj.getClass().getDeclaredFields(); // empty array [] obj.getClass().getName(); // [Ljava.lang.Object; final BigDecimal b = (BigDecimal) obj[0]; //Compilation error: The type of the expression must be an array type but it resolved to Object 
3
  • 2
    If you know the object type,typecast it to that type. Commented Jan 17, 2013 at 17:44
  • @Renjith I don't know the type. In my screenshot there's no particular type. Commented Jan 17, 2013 at 17:47
  • Dont you have mapping class for the table which you are querying? Commented Jan 17, 2013 at 17:53

5 Answers 5

4

obj.getClass().getDeclaredFields() can help you. Generally learn reflection API. If you object bean you can also use Jackarta BeanUtils.

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

3 Comments

In that case the class doesn't have any fields. You might need to get the super class's fields (and it's super class as well)
what super class above the class Object ?
Generally that is true, but for the current situation it was visible from the screenshot that he has an array (I was blind, as well). So, now it is inappropriate answer.
2

Judging from your comments, your Object is and Object array.

So you should first cast the result to an Object array:

Object[] obj = (Object[]) query.list().get(0); 

Then, you should be able to access the first BigDecimal like that:

BigDecimal b = (BigDecimal) obj[0]; 

Probably, you want to add some exception handling.

2 Comments

The type of the expression must be an array type but it resolved to Object
I haven't casted the object though.. let me check
1

It is not an Object, it is an Array of Objects.

BigDecimal firstColumn = (BigDecimal) ((Object[])query.list().get(0))[0]; BigDecimal secondColumn = (BigDecimal) ((Object[])query.list().get(0))[1]; 

That's all.

Comments

1

UPDATE:

You have a resultset with 2 columns.

Object[] result= query.list().get(0); BigDecimal number1 = (BigDecimal) result[0]; BigDecimal number2 = (BigDecimal) result[1]; 

5 Comments

my object is of type Object
The runtime type is not Object. Try obj.getClass().getName() - it will give you the actual runtime type of your object.
obj.getClass().getName() returns [Ljava.lang.Object;.
As far as I can see, you made a JPA query for a certain object. You should cast the result to that object.
@MajidLAISSI [L in the [Ljava.lang.Object shows that it is an array.
1

Get first what class name of that object by

System.out.println(obj.getClass()); 

Since you are running a sql query, result might be an Entity or Object[].

when you came to know retrieved object from query is an Object[] you can iterate like

if( obj instanceof Object[] ) { Object[] objA = (Object[])obj; for(Object atomicObj : objA ) { System.out.println(atomicObj); } } 

It works for all elements which presents in object array. This time you may get BigDecimal, next query might return a String and BigDecimal.

1 Comment

see edit and my other comments : obj.getClass().getName(); --> [Ljava.lang.Object;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.