2

We have a problem where we need to keep the copy of an input file in DB(regulatory purposes). The file can be of size upto 1GB. Is there any way (using streaming and incremental updates) to insert the entire file into DB using JPA ?

nobody
  • 1,909
  • 5
  • 31
  • 45
  • 2
    I would recommend against it. Partly for performance reasons (mostly in inserting) but primarily because it will bloat your database, making backups and export/import operations much, much more time-consuming in the future. If the files are that large, it's better to save them on a regular, backed up drive or on a SAN or NAS. – pap Aug 29 '11 at 13:54

2 Answers2

3

You can use the @Lob annotation on a field, and your JPA provider will figure out the rest. I have used it for

For example:

@Entity
public class TextArticle{

    private Date creationDate;

    @Lob
    private String textContent;

    ...
}

And then later, use it as a String.

EntityManager em;
em.persist(new TextArticle(date, textContent));

If you would prefer to use a BLOB instead of a CLOB, try this:

@Entity
public class TextArticle{

    private Date creationDate;

    @Lob
    private byte[] textBytes;

    ...
}

Check out the Hibernate Documentation

logan
  • 3,416
  • 1
  • 33
  • 45
-1

If the file can really be 1g and you don't have > 1g of RAM, then your best bet is probably raw JDBC using streams on the file and the JDBC Blob.

James
  • 17,965
  • 11
  • 91
  • 146