0

I want to save some file content to database with hibernate, my model for hibernate is:

import javax.persistence.*;
import java.io.Serializable;
import static javax.persistence.AccessType.FIELD;

@Entity
@Access(FIELD)
@Table(name = "MYFILE")
public class MyFile implements Serializable {

    @Id
    @SequenceGenerator(name = "MYSEQ", sequenceName = "MYSEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MYSEQ")
    @Column(name = "ID")
    private Long id;


    @Column(name = "FILE_CONTENT")
    @Lob
    private byte[] fileContent;

    // getters and setters

}

You can see the type of the fileContent field is byte[], and there is a @Lob on it.

It's working well, but the problem is if the files are huge, e.g. 10M, or 20M, it will use a lot of memory, which is an issue in production.

I wonder if there is any way to make it using low memory, say, use a stream instead of byte[]? But I'm not sure what is the correct solution.

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • Why are you wanting to put this in Hibernate? It seems like it might be the wrong tool for the job. – chrylis -cautiouslyoptimistic- Feb 28 '14 at 10:30
  • Is M = Mb ? (10 Mb, 20 Mb). Do you read this column very frequently ? Does it change very frequently ? If it does not change very frequently did you thought of caching it ? – Jay Feb 28 '14 at 10:32
  • See http://stackoverflow.com/questions/9253323/how-to-persist-large-blobs-100mb-in-oracle-using-hibernate – Peter Keller Feb 28 '14 at 10:48
  • @Jay, it's kind of archive, not frequently, and never changed – Freewind Feb 28 '14 at 12:51
  • If its never changed then its a good candidate for Hibernate 2nd level cache with READ_ONLY strategy. This would improve the performance. Search in google for tutorials like http://www.tutorialspoint.com/hibernate/hibernate_caching.htm. If you implement please share the performance difference. – Jay Feb 28 '14 at 13:29
  • GreyBeardedGreek's answer for http://stackoverflow.com/questions/9607307/hibernate-out-of-memory-inserting-big-blob is applicable here as well. – user3360944 Feb 28 '14 at 16:51

0 Answers0