• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can you use JPA to import a csv file?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

This may seem like a weird question ... but I have a JPA project
that I need to modify to allow the import of a csv file into
a mysql database table.

I know mysql allows the import of a csv into it through the "load" command, but I was
wondering (or perhaps curious) how would one go about
importing a csv into a database using JPA commands in a JPA project?

Thanks again
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is doable.

Just think of how you should do if you wanted to represent the data in the cvs-file using JPA. Then create those classes and a bunch of test classes that can create them so that you can verify that they get into the database. That is step one.

Step 2 is to create a parser that reads the cvs-file and creates an instance of the representing type. A factory...
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a better question is "does it make sense to use JPA?". Object mapping is often not the best (read: most performant) approach for bulk DB imports. I would advise to use one of the existing Java CSV parsing libraries, and then use straight JDBC with batching for the insert operations.
 
Marco Di Baggio
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ove and Tim,

I wanted to thank you both for your comments. I understand completely what you are
each trying to say, which is great.

I guess my next question is ... is it exceptable to have a JPA project that utilizes
a straight jdbc operation, say in my example specifically, using striaght
jdbc for the csv import operation?

I have pasted the LOAD INFILE mysql command that imports the CSV.



I guess I am wondering if using straight JDBC in a JPA project is poor
practice or show my lack of tenure?

Thanks once again

 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ORM tools like JPA are not the solution to all DB tasks. ETL (Extract, Transform, Load) tasks are a good example of where ORM is not really applicable.

You should probably use a non-scrolling, non-updatable Resultset, though.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic