0

I am using reflections to map an ResultSet to a Field in bean.

field = clazz.getDeclaredField(str);
field.setAccessible(true);
Object resultSetObject = rs.getObject(str);
Class fieldType = field.getType();
field.set(clazzInst, fieldType.cast(resultSetObject));

The problem is resultSetObject is of type Integer and fieldType is of Long and I cannot cast Intger to Long and getting ClassCastException.

5
  • This question might help you stackoverflow.com/questions/6690745/converting-integer-to-long Commented Jun 30, 2014 at 5:03
  • In that question you explicitly know that you have to cast Integer to Long, but here i don't know about their types Commented Jun 30, 2014 at 5:04
  • Casting doesn't do anything to the type of the object. Commented Jun 30, 2014 at 5:06
  • @SotiriosDelimanolis will you please elaborate on this? Commented Jun 30, 2014 at 5:07
  • Why is the ResultSet-derived object of type Integer while the target field is of type Long? (NB I suspect it's actually the other way around.) The real solution here is to make sure your Java and SQL data models match. Commented Jun 30, 2014 at 5:14

3 Answers 3

0

You are essentially asking to duplicate the type knowledge that is available at compile-time with which the compiler generates the correct conversion. The runtime doesn't have this knowledge, so you will have to provide it by building a matrix of types and coding all the conversions you want, explicitly.

You can define this as a matrix indexed by type along both axes (from-type and to-type) or more likely as a Map whose key is a ConversionType object each instance of which defines fromType, toType and a convert() method.

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

Comments

0

Whether you have

Object ref = new String("object of type string");

or

String ref = new String("still object of type string");

the object referenced will be of type String. Doing

Object obj = (Object) new String("still a string");

does not change that the referenced object is a String. In your case, you'll probably need a conversion strategy to convert between types.

2 Comments

that is interesting, yet could you please share your source so I can learn more? thank you
@kick I don't have any right now. You have to differentiate between instances, references and variables.
0

You still have another option, convert long to string ,and then convert to integer.

5 Comments

But since I don't know their types explicitly, I must be prepared for the cases where fieldType and resultSetObject can be of different Types like Date, int, Double Boolean or any other types
You don't know the exact data type in your database? You can read it from database MetaData.
I can do that but then I have to define a conversion strategy from every possible type to every other types
NO ,YOU ONLY NEED TO DEFINE THE CONVERSION RULE BETWEEN YOUR TYPE WITH STRING.
but why? don't I have to convert string to myType again?

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.