/** * Permet de créer un Blob à partir d'un stream sans indiquer/connaître la taille. Fonctionne uniquement avec jdbc4. Une exception est * lancée si jdbc4 n'est pas présent. * * @param aInputStream * @return */ public Blob createBlob(InputStream aInputStream) { final LobCreator lobCreator = Hibernate.getLobCreator(session); // // JDBC4 -> pas besoin de la taille // if (lobCreator instanceof ContextualLobCreator) { // // Passage de -1 comme taille: De toutes façons cette valeur n'est pas utilisée en jdbc4 // return lobCreator.createBlob(aInputStream, -1); // } // else { // Fallback JDBC3 // On récupère le stream pour connaitre la taille final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { IOUtils.copy(aInputStream, bos); return lobCreator.createBlob(new ByteArrayInputStream(bos.toByteArray()), bos.size()); } catch (IOException ioe) { throw new RuntimeException(ioe); } // } }
private WrapperOptions getOptions(final SessionImplementor session) { return new WrapperOptions() { public boolean useStreamForLobBinding() { return Environment.useStreamsForBinary() || session.getFactory().getDialect().useInputStreamToInsertBlob(); } public LobCreator getLobCreator() { return Hibernate.getLobCreator( session ); } public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) { final SqlTypeDescriptor remapped = sqlTypeDescriptor.canBeRemapped() ? session.getFactory().getDialect().remapSqlTypeDescriptor( sqlTypeDescriptor ) : sqlTypeDescriptor; return remapped == null ? sqlTypeDescriptor : remapped; } }; }
@Override public Blob mergeBlob(Blob original, Blob target, SessionImplementor session) { if ( original == null && target == null ) { return null; } try { final LobCreator lobCreator = session.getFactory().getJdbcServices().getLobCreator( session ); return original == null ? lobCreator.createBlob( ArrayHelper.EMPTY_BYTE_ARRAY ) : lobCreator.createBlob( original.getBinaryStream(), original.length() ); } catch (SQLException e) { throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge BLOB data" ); } }
@Override public Clob mergeClob(Clob original, Clob target, SessionImplementor session) { if ( original == null && target == null ) { return null; } try { final LobCreator lobCreator = session.getFactory().getJdbcServices().getLobCreator( session ); return original == null ? lobCreator.createClob( "" ) : lobCreator.createClob( original.getCharacterStream(), original.length() ); } catch (SQLException e) { throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge CLOB data" ); } }
@Override public NClob mergeNClob(NClob original, NClob target, SessionImplementor session) { if ( original == null && target == null ) { return null; } try { final LobCreator lobCreator = session.getFactory().getJdbcServices().getLobCreator( session ); return original == null ? lobCreator.createNClob( "" ) : lobCreator.createNClob( original.getCharacterStream(), original.length() ); } catch (SQLException e) { throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge NCLOB data" ); } }
@Override public LobCreator getLobCreator(LobCreationContext lobCreationContext) { return lobCreatorBuilder.buildLobCreator( lobCreationContext ); }
private LobCreator lobCreator() { // Always use NonContextualLobCreator. If ContextualLobCreator is // used both here and in WrapperOptions, return NonContextualLobCreator.INSTANCE; }
public PersistedFileAttribute saveFileAttribute(ClientSession session, FileItem fileItem, String forClassName, String forAttributeName, Integer forInstanceID) { PersistedFileAttribute pfa = null; EntityManager em = null; EntityTransaction tx = null; try { em = AtomEMFactory.getEntityManager(); tx = em.getTransaction(); tx.begin(); DomainObject forInstance = null; if(forInstanceID != null) { //forInstance = getDomainObject(session, forInstanceID, forClassName); forInstance = (DomainObject) em.find(ServerTools.getClassForName(forClassName), forInstanceID); } HibernateEntityManager hem = em.unwrap(HibernateEntityManager.class); Session hibernateSession = hem.getSession(); LobCreator lobCreator = Hibernate.getLobCreator(hibernateSession); Blob blob = lobCreator.createBlob(fileItem.getInputStream(), fileItem.getSize()); pfa = new PersistedFileAttribute(blob, fileItem.getName(), fileItem.getContentType(), forClassName, forAttributeName, forInstance, session.getUser()); pfa = em.merge(pfa); tx.commit(); } catch (Throwable t) { if(tx != null && tx.isActive()) tx.rollback(); if (t instanceof AtomException) { throw (AtomException) t; } else { AtomTools.log(Level.SEVERE, "ServerTools.saveFileAttribute exception: " + t.getClass().getSimpleName() + " - " + t.getMessage(), this, t); } } finally { ServerTools.closeDBConnection(tx, em); } return pfa; }
@Override public LobCreator getLobCreator() { return target.getLobCreator(); }
/** * Create an instance of a {@link LobCreator} appropriate for the current environment, mainly meant to account for * variance between JDBC 4 (<= JDK 1.6) and JDBC3 (>= JDK 1.5). * * @param lobCreationContext The context in which the LOB is being created * @return The LOB creator. */ public LobCreator getLobCreator(LobCreationContext lobCreationContext);
/** * Build a LobCreator using the given context * * @param lobCreationContext The LOB creation context * * @return The LobCreator */ public LobCreator buildLobCreator(LobCreationContext lobCreationContext) { return useContextualLobCreation ? new ContextualLobCreator( lobCreationContext ) : NonContextualLobCreator.INSTANCE; }
/** * Obtain a lob creator for the given session. * * @param session The session for which to obtain a lob creator * * @return The log creator reference */ public static LobCreator getLobCreator(Session session) { return getLobCreator( (SessionImplementor) session ); }
/** * Obtain a lob creator for the given session. * * @param session The session for which to obtain a lob creator * * @return The log creator reference */ public static LobCreator getLobCreator(SessionImplementor session) { return session.getFactory() .getJdbcServices() .getLobCreator( session ); }
/** * Obtain access to the {@link LobCreator} * * @return The LOB creator */ public LobCreator getLobCreator();
/** * Obtain access to the {@link LobCreator} * * @return The LOB creator */ LobCreator getLobCreator();