Created
November 9, 2012 15:38
-
-
Save rkroll/4046374 to your computer and use it in GitHub Desktop.
Hibernate Batch export problem example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExportCSV extends ApplicationContextAwareTest { | |
private static final Logger logger = LoggerFactory.getLogger(ExportCSV.class); | |
@Autowired | |
private TaxRollRecordDAO taxRollRecordDAO; | |
String query = "from TaxRollRecord r order by r.id"; | |
String countQuery = "select count(r) from TaxRollRecord r"; | |
@Test | |
public void doExport() { | |
Long count = taxRollRecordDAO.getCount(countQuery); | |
logger.debug("Exporting {} records", count); | |
int offset = 0; | |
int limit = 5000; | |
CSVWriter writer = null; | |
try { | |
writer = new CSVWriter(new FileWriter("tax_roll_export.csv"), '\t'); | |
writer.writeNext(headerRow()); | |
while (true) { | |
logger.debug("Processing offset '{}' of '{}'", offset, count); | |
List<TaxRollRecord> data = taxRollRecordDAO.find(query, offset, limit); | |
for (TaxRollRecord record : data) { | |
String[] line = toLine(record); | |
writer.writeNext(line); | |
} | |
if (data.size() < limit) { | |
break; | |
} | |
offset += limit; | |
} | |
} | |
catch (Exception e) { | |
logger.error("Error writing CSV file", e); | |
} | |
finally { | |
IOUtils.closeQuietly(writer); | |
} | |
} | |
private String[] toLine(TaxRollRecord record) { | |
List<String> line = new ArrayList<String>(24); | |
line.add(record.getCity()); | |
line.add(record.getState()); | |
line.add(record.getZip()); | |
line.add(ObjectUtils.toString(record.getBeds())); | |
line.add(ObjectUtils.toString(record.getBaths())); | |
line.add(ObjectUtils.toString(record.getYearBuilt())); | |
line.add(ObjectUtils.toString(record.getAdjustedBuildingSqft())); | |
line.add(ObjectUtils.toString(record.getSaleDate1())); | |
line.add(ObjectUtils.toString(record.getSalePrice1())); | |
line.add(ObjectUtils.toString(record.getSaleDate2())); | |
line.add(ObjectUtils.toString(record.getSalePrice2())); | |
line.add(ObjectUtils.toString(record.getSaleDate3())); | |
line.add(ObjectUtils.toString(record.getSalePrice3())); | |
line.add(ObjectUtils.toString(record.getSaleDate4())); | |
line.add(ObjectUtils.toString(record.getSalePrice4())); | |
line.add(ObjectUtils.toString(record.getJustLandValue())); | |
line.add(ObjectUtils.toString(record.getJustBuildingValue())); | |
line.add(ObjectUtils.toString(record.getAssessedValue())); | |
line.add(ObjectUtils.toString(record.getLastYearLandValue())); | |
line.add(ObjectUtils.toString(record.getLastYearBuildingValue())); | |
line.add(ObjectUtils.toString(record.getLastYearAssessedValue())); | |
line.add(ObjectUtils.toString(record.getTwoYearsLandValue())); | |
line.add(ObjectUtils.toString(record.getTwoYearsBuildingValue())); | |
line.add(ObjectUtils.toString(record.getTwoYearsAssessedValue())); | |
return line.toArray(new String[line.size()]); | |
} | |
private String[] headerRow() { | |
List<String> line = new ArrayList<String>(24); | |
line.add("city"); | |
line.add("state"); | |
line.add("zip"); | |
line.add("beds"); | |
line.add("baths"); | |
line.add("year_built"); | |
line.add("sqft"); | |
line.add("sale_date_1"); | |
line.add("sale_price_1"); | |
line.add("sale_date_2"); | |
line.add("sale_price_2"); | |
line.add("sale_date_3"); | |
line.add("sale_price_3"); | |
line.add("sale_date_4"); | |
line.add("sale_price_4"); | |
line.add("just_land_value"); | |
line.add("just_building_value"); | |
line.add("assessed_value"); | |
line.add("last_year_land_value"); | |
line.add("last_year_building_value"); | |
line.add("last_year_assessed_value"); | |
line.add("two_years_land_value"); | |
line.add("two_years_building_value"); | |
line.add("two_years_assessed_value"); | |
String[] arr = line.toArray(new String[line.size()]); | |
line.clear(); | |
return arr; | |
} | |
} | |
public interface GenericDAO<T, ID extends Serializable> { | |
T findById(ID id, boolean lock); | |
List<T> findAll(); | |
List<T> findByExample(T exampleInstance, String[] excludeProperty); | |
T save(T entity); | |
void makeTransient(T entity); | |
List<T> find(final String query, final int offset, final int limit, final Object... values); | |
public long getCount(final String query, final Object... values); | |
public long getCount(final String query); | |
public void flush(); | |
public void clear(); | |
public void evict(T entity); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment