44 */
55package org .hibernate .orm .test .envers .integration .blob ;
66
7- import static org .hamcrest .MatcherAssert .assertThat ;
8- import static org .hibernate .testing .transaction .TransactionUtil .doInJPA ;
9- import static org .junit .Assert .fail ;
10-
11- import java .io .BufferedInputStream ;
12- import java .io .InputStream ;
13- import java .nio .file .Files ;
14- import java .nio .file .Path ;
15- import java .sql .Blob ;
16-
7+ import jakarta .persistence .Entity ;
8+ import jakarta .persistence .GeneratedValue ;
9+ import jakarta .persistence .Id ;
1710import org .hamcrest .Matchers ;
1811import org .hibernate .engine .jdbc .proxy .BlobProxy ;
1912import org .hibernate .envers .Audited ;
2013import org .hibernate .orm .test .envers .BaseEnversJPAFunctionalTestCase ;
2114import org .hibernate .orm .test .envers .Priority ;
2215import org .junit .Test ;
2316
24- import jakarta .persistence .Entity ;
25- import jakarta .persistence .GeneratedValue ;
26- import jakarta .persistence .Id ;
17+ import java .io .BufferedInputStream ;
18+ import java .io .InputStream ;
19+ import java .nio .file .Files ;
20+ import java .nio .file .Path ;
21+ import java .sql .Blob ;
22+
23+ import static org .hamcrest .MatcherAssert .assertThat ;
24+ import static org .hibernate .testing .transaction .TransactionUtil .doInJPA ;
25+ import static org .junit .Assert .fail ;
2726
2827/**
2928 * @author Chris Cranford
@@ -32,19 +31,19 @@ public class BasicBlobTest extends BaseEnversJPAFunctionalTestCase {
3231
3332 @ Override
3433 protected Class <?>[] getAnnotatedClasses () {
35- return new Class <?>[] { Asset .class };
34+ return new Class <?>[] {Asset .class };
3635 }
3736
3837 @ Test
3938 @ Priority (10 )
4039 public void initData () {
41- final Path path = Path .of ( getClass ().getResource ( "./blob.txt" ).getPath () );
40+ final Path path = Path .of ( Thread .currentThread ().getContextClassLoader ()
41+ .getResource ( "org/hibernate/orm/test/envers/integration/blob/blob.txt" ).getPath () );
4242 doInJPA ( this ::entityManagerFactory , entityManager -> {
43- try {
44- final Asset asset = new Asset ();
45- asset .setFileName ( "blob.txt" );
43+ final Asset asset = new Asset ();
44+ asset .setFileName ( "blob.txt" );
4645
47- final InputStream stream = new BufferedInputStream ( Files .newInputStream ( path ) );
46+ try ( final InputStream stream = new BufferedInputStream ( Files .newInputStream ( path ) )) {
4847 assertThat ( stream .markSupported (), Matchers .is ( true ) );
4948
5049 // We use the method readAllBytes instead of passing the raw stream to the proxy
@@ -70,6 +69,37 @@ public void initData() {
7069 fail ( "Failed to persist the entity" );
7170 }
7271 } );
72+
73+ try (final InputStream stream = new BufferedInputStream ( Files .newInputStream ( path ) )) {
74+ doInJPA ( this ::entityManagerFactory , entityManager -> {
75+ final Asset asset = new Asset ();
76+ asset .setFileName ( "blob.txt" );
77+
78+ assertThat ( stream .markSupported (), Matchers .is ( true ) );
79+
80+ // We use the method readAllBytes instead of passing the raw stream to the proxy
81+ // since this is the only guaranteed way that will work across all dialects in a
82+ // deterministic way. Postgres and Sybase will automatically close the stream
83+ // after the blob has been written by the driver, which prevents Envers from
84+ // then writing the contents of the stream to the audit table.
85+ //
86+ // If the driver and dialect are known not to close the input stream after the
87+ // contents have been written by the driver, then it's safe to pass the stream
88+ // here instead and the stream will be automatically marked and reset so that
89+ // Envers can serialize the data after Hibernate has done so. Dialects like
90+ // H2, MySQL, Oracle, SQL Server work this way.
91+ //
92+ //
93+ Blob blob = BlobProxy .generateProxy ( stream , 200L );
94+
95+ asset .setData ( blob );
96+ entityManager .persist ( asset );
97+ } );
98+ }
99+ catch (Exception e ) {
100+ e .printStackTrace ();
101+ fail ( "Failed to persist the entity" );
102+ }
73103 }
74104
75105 @ Audited
0 commit comments