Skip to content

Commit

Permalink
HHH-14725 Adjust test to use byte array, update Javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros authored and beikov committed Dec 18, 2024
1 parent 3f93ec8 commit 6300c34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public static Blob generateProxy(byte[] bytes) {
/**
* Generates a BlobImpl proxy using a given number of bytes from an InputStream.
*
* Be aware that certain database drivers will automatically close the provided InputStream after the
* contents have been written to the database. This may cause unintended side effects if the entity
* is also audited by Envers. In this case, it's recommended to use {@link #generateProxy(byte[])}
* instead as it isn't affected by this non-standard behavior.
*
* @param stream The input stream of bytes to be created as a Blob.
* @param length The number of bytes from stream to be written to the Blob.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,20 @@ public void initData() {
final InputStream stream = new BufferedInputStream( Files.newInputStream( path ) );
assertThat( stream.markSupported(), Matchers.is( true ) );

Blob blob = BlobProxy.generateProxy( stream, Files.size( path ) );
// We use the method readAllBytes instead of passing the raw stream to the proxy
// since this is the only guaranteed way that will work across all dialects in a
// deterministic way. Postgres and Sybase will automatically close the stream
// after the blob has been written by the driver, which prevents Envers from
// then writing the contents of the stream to the audit table.
//
// If the driver and dialect are known not to close the input stream after the
// contents have been written by the driver, then it's safe to pass the stream
// here instead and the stream will be automatically marked and reset so that
// Envers can serialize the data after Hibernate has done so. Dialects like
// H2, MySQL, Oracle, SQL Server work this way.
//
//
Blob blob = BlobProxy.generateProxy( stream.readAllBytes() );

asset.setData( blob );
entityManager.persist( asset );
Expand Down

0 comments on commit 6300c34

Please sign in to comment.