I need to select a hundred of parameters from database then return it in a DTO to create an excel file.
I'm using spring data, i think have two solutions:
using multiselect method then define a constructor in DTO with all parameters(the number of parameter is more than 100)
using multiselect method and map the array (Object[]) returned to a DTO=>mapping is hard to implement and not scalable.
Do you have any other idea please?
Thank you.
CodePudding user response:
If you have more than 100 parameters, that's a lot for constructor. How do you get these parameters? maybe you can use a (key,value) map instead?
If the model is complicated then maybe you can use plain old JDBC? Then you can easily put these parameters into the query.
Plain old ResultSet it is quite easy to work with, you can get each row and column one after another and check their type, you can build the Excel file line by line then. Skip instantiating the DTO.
CodePudding user response:
I agree with the sentiment of Krzysztof Cichocki. Don't use JPA for this. JPA is for loading an object graph, modifying it and persisting the changes again. It generates to much overhead for things like generating a report.
Instead of using plain JDBC, I recommend using JdbcTemplate
or NamedParameterJdbcTemplate
.
The methods taking a RowCallBackHandler
seem the right choice for your problem.