Save file in mysql database using hibernate

I am trying to figure out the β€œcorrect”, safe way to save / retrieve files in a mysql database using Hibernate. The solution should work with files that are large enough, so it is important to minimize the amount of memory.

My decision:

The model class containing the java.sql.Blob field, the getter is annotated using "@Lob".

public static void main(String[] args) {
    DAOFactory factory = DAOFactory.getFactory();
    IResultExtraDAO resultExtraDAO = factory.getResultExtraDAO();
    factory.getResultExtraDAO().beginTransaction();

    ResultExtra resultExtra = new ResultExtra();
    LobHelper lh = HibernateUtil.getSession().getLobHelper();
    try {
        File file = new File("/3030.jpg");
        FileInputStream fis = new FileInputStream(file);
        Blob b = lh.createBlob(fis, fis.available());
        resultExtra.setFile(b);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    resultExtraDAO.save(resultExtra);

    factory.getResultExtraDAO().commitTransaction();
}

Is this the right way to do this? If there is a risk of running out of memory, if are manly simultaneous downloads and / or large files? Is there any other solution that would be better?

, "" ", habernate, HibernateUtil , , . .

+5
1

Blob . , . , , .

, , Jackrabbit. URL-, , .

, , , , , , , Hibernate lazy-load blobs, . .

+1

All Articles