0

I'm trying to generate Java POJO classes automatically from csv files where the first line contains headers and the other ones contain data. I found many examples and tools in the net showing how to parse csv files and put data into Java objects but in all of them Java POJO classes have already been created manually and I'm looking for a tool which generates them automatically. Otherwise I need a tool similar to Hibernate reverse engineering Tools but instead of databases I've csv files.

For example here is my input csv file:

username, password,   date,        zip,  town

Klaus,    qwexyKiks,  17/1/2007,1111, New York

Oufu1,     bobilops,    10/10/2007,4555, New York

And here is what I need to generate automatically:

package test;

import java.util.Date;

public class UserBean {
    String username;
    String password;
    Date date;
    int zip;
    String town;


    public Date getDate() {
        return date;
    }
    public String getPassword() {
        return password;
    }
    public String getTown() {
        return town;
    }
    public String getUsername() {
        return username;
    }
    public int getZip() {
        return zip;
    }
    public void setDate(final Date date) {
        this.date = date;
    }
    public void setPassword(final String password) {
        this.password = password;
    }

    public void setTown(final String town) {
        this.town = town;
    }
    public void setUsername(final String username) {
        this.username = username;
    }
    public void setZip(final int zip) {
        this.zip = zip;
    }
}
claude88
  • 41
  • 2
  • 6

1 Answers1

-1

I think that what you want to do would be quite slow and not fault-tolerant.

The main difference between tables in relational database management systems and csv files is that csv files do not have data type information. So the software that you would like to have shoud apply type inference on columns and that could be very slow and error prone. If you are going to parse big files that may contain errors, you wouldn't have a validation functionality, and execution tyme could be a bottleneck to the whole system.

Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49
  • I need to convert my csv files to POJO classes because my application have many different data sources (databases, csv, xml ...) so my business layer have to be independent of the type of data source and uses only POJO classes that's why I'm generating POJO classes using Hibernate Tools from databases and now I'm looking for a tool to do the same work as Hibernate with csv files. I agree with you concerning the data type problem but this can be resolved by creating only String objects in the access layer and do some type convertion in Business layer. – claude88 May 14 '12 at 18:03
  • Do you mean that you want to implement the [Data Transfer Object](http://martinfowler.com/eaaCatalog/dataTransferObject.html) pattern (also known as Value Object)? That could be an idea, but you would have to perform input validation inside the business layer. Have you tried [CSV JDBC Drivers](http://stackoverflow.com/questions/1468998/execute-sql-on-csv-files-via-jdbc)? – Vitaly Olegovitch May 14 '12 at 18:45
  • Thank you for mentioning the Data Transfer Object pattern, actually I'm nearly implementing the same thing, it will be a good idea if I include it into my project. For the CSV JDBC Drivers I don't know if I can use Hibernate or another tool to create POJO classes from databases representing csv files, I think this makes things more complicated, so I'm looking for a direct converter tool (without using databases) and if there is no existing one, I'll have to develop my own converter. – claude88 May 15 '12 at 09:07