Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

In this page you can find the example usage for org.hibernate Session refresh.

Prototype

void refresh(Object object, LockOptions lockOptions);

Source Link

Document

Re-read the state of the given instance from the underlying database, with the given LockMode.

Usage

From source file:org.squale.jraf.provider.persistence.hibernate.BlobHelper.java

License:Open Source License

public final static void writeByte2BLOB(ISession session, Object bo, String property, byte[] bytes)
        throws JrafPersistenceException {

    // recuperation de la session hibernate
    Session hSession = ((SessionImpl) session).getSession();

    try {//from  w  ww .j a v  a2 s  .  co  m
        // ecriture du BLOB vide
        createEmptyBlob(hSession, bo, property);

        // refresh de l'objet
        hSession.refresh(bo, LockMode.UPGRADE);

        // recuperation du BLOB
        BLOB oracleBLOB = (BLOB) PropertyUtils.getProperty(bo, property);

        // ouverture du stream d'ecriture sur le BLOB
        OutputStream blobOutputStream = oracleBLOB.getBinaryOutputStream();

        blobOutputStream.write(bytes, 0, bytes.length);
        blobOutputStream.flush();
        blobOutputStream.close();
    } catch (Exception e) {
        log.error("Probleme lors de l'ecriture des donnees dans le BLOB", e);
        throw new JrafPersistenceException("Probleme lors de l'ecriture des donnees dans le BLOB", e);
    }

}

From source file:org.squale.jraf.provider.persistence.hibernate.BlobHelper.java

License:Open Source License

/**
 * Ecriture d'un fichier dans un BLOB/*from  w w  w . j  a va  2  s  .co m*/
 * @param session session de persistance
 * @param bo business object
 * @param property propriete
 * @param file fichier
 * @throws JrafPersistenceException
 */
public final static void writeFile2BLOB(ISession session, Object bo, String property, File file)
        throws JrafPersistenceException {

    // recuperation de la session hibernate
    Session hSession = ((SessionImpl) session).getSession();

    long fSize = file.length();
    byte[] buffer = null;

    try {
        // ecriture du BLOB vide
        createEmptyBlob(hSession, bo, property);

        // refresh de l'objet
        hSession.refresh(bo, LockMode.UPGRADE);

        // recuperation du BLOB
        BLOB oracleBLOB = (BLOB) PropertyUtils.getProperty(bo, property);

        // ouverture du stream d'ecriture sur le BLOB
        OutputStream blobOutputStream = oracleBLOB.getBinaryOutputStream();

        InputStream sampleFileStream = new FileInputStream(file);

        // cas tres gros fichier
        if (fSize > Integer.MAX_VALUE) {
            buffer = new byte[Integer.MAX_VALUE];
        } else {
            buffer = new byte[(int) file.length()];
        }

        int nread = 0;
        while ((nread = sampleFileStream.read(buffer)) != -1)
            blobOutputStream.write(buffer, 0, nread);
        sampleFileStream.close();
        blobOutputStream.flush();
        blobOutputStream.close();

    } catch (Exception e) {
        log.error("Probleme lors de l'ecriture des donnees dans le BLOB", e);
        throw new JrafPersistenceException("Probleme lors de l'ecriture des donnees dans le BLOB", e);
    }

}