Load CSV file in Spring JPA using @Query

Viewed 1631

I'm trying to upload a CSV file directly into the database using Hibernate + Spring Data JPA.

When I run this command con MySQL shell, everything works fine:

LOAD DATA LOCAL INFILE '' INTO TABLE debtor_csv FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (name, cuil, bcra, person_type, address, location, province, zip_code, office, phone, segment, plan);

I need to do this from my Java Web Application. User uploads a CSV file and I drop it into an auxiliary table before processing it. But when I try to do it from DAO object, it just simply doesn't work.

These are the things that I've tried:

@Query(value = "LOAD DATA INFILE :filename INTO TABLE debt_csv FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (debtor, cuil, date, amount, op_number, description);",
        nativeQuery = true)
void uploadFile(String filename);


@Query(value = "LOAD DATA INFILE :filename INTO TABLE debt_csv FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (debtor, cuil, date, amount, op_number, description);",
        nativeQuery = true)
void uploadFile(@Param("filename") String filename);


@Query(value = "LOAD DATA INFILE 'filename' INTO TABLE debt_csv FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (debtor, cuil, date, amount, op_number, description);",
        nativeQuery = true)
void uploadFile(String filename);


@Query(value = "LOAD DATA INFILE <absolute_filepath_hardcoded> INTO TABLE debt_csv FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (debtor, cuil, date, amount, op_number, description);",
        nativeQuery = true)
void uploadFile();

And the same using LOAD DATA LOCAL INFILE.

Any suggestions?

0 Answers
Related