@Override public Blob createBlob(InputStream uploadedFileInputStream, long fileSize) throws IOException { InputStream inputStream = uploadedFileInputStream; //We'll work with Blob instances and streams so that the uploaded files are never read into memory return ((HibernateEntityManager) em).getSession().getLobHelper().createBlob(inputStream, fileSize); }
@Test public void hibernateTest() { printConfig(); Session session = openSession(); session.beginTransaction(); LobTestEntity entity = new LobTestEntity(); entity.setId(1L); entity.setLobValue(session.getLobHelper().createBlob(new byte[9999])); entity.setQwerty(randomString(4000)); session.save(entity); session.getTransaction().commit(); }
s.beginTransaction(); Blobber b = new Blobber(); b.setBlob( s.getLobHelper().createBlob( "foo/bar/baz".getBytes() ) ); b.setClob( s.getLobHelper().createClob("foo/bar/baz") ); s.save(b); s.beginTransaction(); b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) ); b.setClob( s.getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") ); s.flush(); s.getTransaction().commit();
@Test public void testMergingBlobData() throws Exception { final byte[] original = BlobLocatorTest.buildByteArray( LOB_SIZE, true ); final byte[] updated = BlobLocatorTest.buildByteArray( LOB_SIZE, false ); Session s = openSession(); s.beginTransaction(); LobHolder entity = new LobHolder(); entity.setBlobLocator( s.getLobHelper().createBlob( original ) ); s.save( entity ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); // entity still detached... entity.setBlobLocator( s.getLobHelper().createBlob( updated ) ); entity = (LobHolder) s.merge( entity ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); entity = (LobHolder) s.get( LobHolder.class, entity.getId() ); assertEquals( "blob sizes did not match after merge", LOB_SIZE, entity.getBlobLocator().length() ); assertTrue( "blob contents did not match after merge", Arrays.equals( updated, BlobLocatorTest.extractData( entity.getBlobLocator() ) ) ); s.delete( entity ); s.getTransaction().commit(); s.close(); }
entity.setClobLocator( s.getLobHelper().createClob( original ) ); s.save( entity ); s.getTransaction().commit(); s.beginTransaction(); entity.setClobLocator( s.getLobHelper().createClob( updated ) ); entity = (LobHolder) s.merge( entity ); s.flush();
s.beginTransaction(); Blobber b = new Blobber(); b.setBlob( s.getLobHelper().createBlob( "foo/bar/baz".getBytes() ) ); b.setClob( s.getLobHelper().createClob("foo/bar/baz") ); s.save(b); s.beginTransaction(); b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) ); b.setClob( s.getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") ); s.flush(); s.getTransaction().commit();
@Test public void testBlob() throws SQLException { final int id = doInHibernate( this::sessionFactory, s -> { FileBlob file = new FileBlob(); file.setBlob( s.getLobHelper().createBlob( "TEST CASE".getBytes() ) ); // merge transient entity file = (FileBlob) s.merge( file ); return file.getId(); } ); doInHibernate( this::sessionFactory, s -> { FileBlob file = s.get( FileBlob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "blob" ) ); Blob blob = file.getBlob(); try { assertTrue( Arrays.equals( "TEST CASE".getBytes(), blob.getBytes( 1, (int) file.getBlob().length() ) ) ); } catch (SQLException ex) { fail( "could not determine Lob length" ); } }); }
@Test public void testClob() { final int id = doInHibernate( this::sessionFactory, s -> { FileClob file = new FileClob(); file.setClob( s.getLobHelper().createClob( "TEST CASE" ) ); // merge transient entity file = (FileClob) s.merge( file ); return file.getId(); } ); doInHibernate( this::sessionFactory, s -> { FileClob file = s.get( FileClob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) ); Clob clob = file.getClob(); try { final char[] chars = new char[(int) file.getClob().length()]; clob.getCharacterStream().read( chars ); assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) ); } catch (SQLException ex ) { fail( "could not determine Lob length" ); } catch (IOException ex) { fail( "could not read Lob" ); } }); }
@Test @RequiresDialectFeature(DialectChecks.SupportsNClob.class) public void testNClob() { final int id = doInHibernate( this::sessionFactory, s -> { FileNClob file = new FileNClob(); file.setClob( s.getLobHelper().createNClob( "TEST CASE" ) ); // merge transient entity file = (FileNClob) s.merge( file ); return file.getId(); }); doInHibernate( this::sessionFactory, s -> { FileNClob file = s.get( FileNClob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) ); NClob nClob = file.getClob(); try { final char[] chars = new char[(int) file.getClob().length()]; nClob.getCharacterStream().read( chars ); assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) ); } catch (SQLException ex ) { fail( "could not determine Lob length" ); } catch (IOException ex) { fail( "could not read Lob" ); } }); }
/** * Retrieve this session's helper/delegate for creating LOB instances. * * @return This session's LOB helper */ public LobHelper getLobHelper() { return getSession().getLobHelper(); }
Document d = new DocumentImpl(); d.setName("Hibernate in Action"); d.setContent( s.getLobHelper().createBlob( "blah blah blah".getBytes() ) ); Long did = (Long) s.save(d); SecureDocument d2 = new SecureDocumentImpl(); d2.setName("Secret"); d2.setContent( s.getLobHelper().createBlob( "wxyz wxyz".getBytes() ) );
public LobHelper getLobHelper() { initSession(); return session.getLobHelper(); }
public LobHelper getLobHelper() { try { return getDelegate().getLobHelper(); } catch (HibernateException ex) { throw handleException(ex); } }
public Blob createBlob(final byte[] bytes) { final Session session = getSession(); final LobHelper lobHelper = session.getLobHelper(); return lobHelper.createBlob(bytes); }
public Clob createClob(final String string) { final Session session = getSession(); final LobHelper lobHelper = session.getLobHelper(); return lobHelper.createClob(string); }
public Blob createBlob(final InputStream in, final long length) { final Session session = getSession(); final LobHelper lobHelper = session.getLobHelper(); return lobHelper.createBlob(in, length); }
@Override public Blob createBlob(MultipartFile uploadedFile) throws IOException { Long size = uploadedFile.getSize(); InputStream inputStream = uploadedFile.getInputStream(); //We'll work with Blob instances and streams so that the uploaded files are never read into memory return ((HibernateEntityManager) em).getSession().getLobHelper().createBlob(inputStream, size); }
@Override @TransactionAttribute(REQUIRES_NEW) public void persistDriftFileData(JPADriftFile driftFile, InputStream data, long numBytes) throws Exception { JPADriftFileBits df = entityManager.find(JPADriftFileBits.class, driftFile.getHashId()); if (null == df) { throw new IllegalArgumentException("JPADriftFile not found [" + driftFile.getHashId() + "]"); } Session session = (Session)entityManager.getDelegate(); df.setDataSize(numBytes); df.setData(session.getLobHelper().createBlob(new BufferedInputStream(data), numBytes)); df.setStatus(LOADED); }
protected T createContent(InputStream in, long length, String hashSha1) { DeflaterInputStream inZip = new DeflaterInputStream(in, new Deflater(Deflater.BEST_COMPRESSION)); T fileEntity = createInstance(); fileEntity.setContent(getSession().getLobHelper().createBlob(inZip, length)); fileEntity.setHashSha1(hashSha1); fileEntity.setSize(length); fileEntity.setInclusionDate(new Date()); return fileEntity; }
@Override public BlobObject save(InputStream inputStream, String fileName, String contentType, long length, BlobObject blob) { HibernateBlob o = (HibernateBlob)blob; o.setBlobData(hbStore.getSession().getLobHelper().createBlob(inputStream, length)); o.setFileName(fileName); o.setContentType(contentType); hbStore.save(o); return o; }