-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-14725 Fix reset handling on BlobProxy #9402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...te-envers/src/test/java/org/hibernate/orm/test/envers/integration/blob/BasicBlobTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.envers.integration.blob; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.Id; | ||
| import org.hamcrest.Matchers; | ||
| import org.hibernate.dialect.PostgreSQLDialect; | ||
| import org.hibernate.dialect.SQLServerDialect; | ||
| import org.hibernate.engine.jdbc.proxy.BlobProxy; | ||
| import org.hibernate.envers.Audited; | ||
| import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; | ||
| import org.hibernate.orm.test.envers.Priority; | ||
| import org.hibernate.testing.SkipForDialect; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.sql.Blob; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| /** | ||
| * @author Chris Cranford | ||
| */ | ||
| public class BasicBlobTest extends BaseEnversJPAFunctionalTestCase { | ||
|
|
||
| @Override | ||
| protected Class<?>[] getAnnotatedClasses() { | ||
| return new Class<?>[] {Asset.class}; | ||
| } | ||
|
|
||
| @Test | ||
| @Priority(10) | ||
| public void testGenerateProxyNoStream() { | ||
| final Path path = Path.of( Thread.currentThread().getContextClassLoader() | ||
| .getResource( "org/hibernate/orm/test/envers/integration/blob/blob.txt" ).getPath() ); | ||
| doInJPA( this::entityManagerFactory, entityManager -> { | ||
| final Asset asset = new Asset(); | ||
| asset.setFileName( "blob.txt" ); | ||
|
|
||
| try (final InputStream stream = new BufferedInputStream( Files.newInputStream( path ) )) { | ||
| assertThat( stream.markSupported(), Matchers.is( true ) ); | ||
|
|
||
| // 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 ); | ||
| } | ||
| catch (Exception e) { | ||
| e.printStackTrace(); | ||
| fail( "Failed to persist the entity" ); | ||
| } | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| @Priority(10) | ||
| @SkipForDialect(value = PostgreSQLDialect.class, | ||
| comment = "The driver closes the stream, so it cannot be reused by envers") | ||
| @SkipForDialect(value = SQLServerDialect.class, | ||
| comment = "The driver closes the stream, so it cannot be reused by envers") | ||
| public void testGenerateProxyStream() { | ||
| final Path path = Path.of( Thread.currentThread().getContextClassLoader() | ||
| .getResource( "org/hibernate/orm/test/envers/integration/blob/blob.txt" ).getPath() ); | ||
|
|
||
| try (final InputStream stream = new BufferedInputStream( Files.newInputStream( path ) )) { | ||
| doInJPA( this::entityManagerFactory, entityManager -> { | ||
| final Asset asset = new Asset(); | ||
| asset.setFileName( "blob.txt" ); | ||
|
|
||
| assertThat( stream.markSupported(), Matchers.is( true ) ); | ||
|
|
||
| // 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, 9192L ); | ||
|
|
||
| asset.setData( blob ); | ||
| entityManager.persist( asset ); | ||
| } ); | ||
| } | ||
| catch (Exception e) { | ||
| e.printStackTrace(); | ||
| fail( "Failed to persist the entity" ); | ||
| } | ||
| } | ||
|
|
||
| @Audited | ||
| @Entity(name = "Asset") | ||
| public static class Asset { | ||
| @Id | ||
| @GeneratedValue | ||
| private Integer id; | ||
| private String fileName; | ||
| private Blob data; | ||
|
|
||
| public Integer getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Integer id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getFileName() { | ||
| return fileName; | ||
| } | ||
|
|
||
| public void setFileName(String fileName) { | ||
| this.fileName = fileName; | ||
| } | ||
|
|
||
| public Blob getData() { | ||
| return data; | ||
| } | ||
|
|
||
| public void setData(Blob data) { | ||
| this.data = data; | ||
| } | ||
| } | ||
|
|
||
| } | ||
31 changes: 31 additions & 0 deletions
31
hibernate-envers/src/test/resources/org/hibernate/orm/test/envers/integration/blob/blob.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?xml version='1.0' encoding='utf-8'?> | ||
| <!-- | ||
| ~ SPDX-License-Identifier: LGPL-2.1-or-later | ||
| ~ Copyright Red Hat Inc. and Hibernate Authors | ||
| --> | ||
| <!DOCTYPE hibernate-configuration PUBLIC | ||
| "-//Hibernate/Hibernate Configuration DTD//EN" | ||
| "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> | ||
|
|
||
| <hibernate-configuration> | ||
| <session-factory> | ||
| <property name="hbm2ddl.auto">create-drop</property> | ||
|
|
||
| <property name="show_sql">false</property> | ||
| <property name="format_sql">false</property> | ||
|
|
||
| <property name="dialect">org.hibernate.dialect.H2Dialect</property> | ||
| <property name="connection.url">jdbc:h2:mem:envers</property> | ||
| <property name="connection.driver_class">org.h2.Driver</property> | ||
| <property name="connection.username">sa</property> | ||
| <property name="connection.password"></property> | ||
|
|
||
| <!--<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>--> | ||
| <!--<property name="connection.url">jdbc:mysql:///hibernate_tests?useUnicode=true&characterEncoding=UTF-8</property>--> | ||
| <!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>--> | ||
| <!--<property name="connection.username">root</property>--> | ||
| <!--<property name="connection.password"></property>--> | ||
|
|
||
| <!--<property name="hibernate.jdbc.batch_size">100</property>--> | ||
| </session-factory> | ||
| </hibernate-configuration> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.