Reputation: 1
In my application we are using resultset to query data base and then construct an object something like:
item.setType(resultset.getString(TYPE));
item.setValue(resultset.getString(PRICE));
Now the problem is that my item class has around 120 fields and I don't want to write 120 lines of code just to set the values. Do we have any cleaner and compact approach ?
Upvotes: 0
Views: 83
Reputation: 86774
You essentially have 4 choices:
Write the 120 lines of code. This is the simplest approach but as you discovered can be tedious.
Use an ORM like Hibernate. If you're not already familiar with it you will have several months of learning curve. From my personal experience it can work very well in online applications, but for batch processing the automatic management of object graphs gets in the way.
Use something like Spring Data. This is sort of half-way between options 1 and 2. It uses reflection and ResultSetMetaData
queries to map data from your resultset to the DAOs you provide. This has much less of a learning curve than full-blown Hibernate (or other ORM) but can be a real time-saver if it meets your needs.
Write your own reflection/ResultSetMetaData based code to do the mapping. If you're going to do this, you're MUCH better off using Spring Data or similar tools. NOT RECOMMENDED.
Upvotes: 2