-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-14725 Fix reset handling on BlobProxy #4496
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
111 changes: 111 additions & 0 deletions
111
...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,111 @@ | ||
| /* | ||
| * Hibernate, Relational Persistence for Idiomatic Java | ||
| * | ||
| * License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
| * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
| */ | ||
| package org.hibernate.orm.test.envers.integration.blob; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.sql.Blob; | ||
|
|
||
| import org.hamcrest.Matchers; | ||
| import org.hibernate.engine.jdbc.BlobProxy; | ||
| import org.hibernate.envers.Audited; | ||
| import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; | ||
| import org.hibernate.orm.test.envers.Priority; | ||
| import org.junit.Test; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.Id; | ||
|
|
||
| /** | ||
| * @author Chris Cranford | ||
| */ | ||
| public class BasicBlobTest extends BaseEnversJPAFunctionalTestCase { | ||
|
|
||
| @Override | ||
| protected Class<?>[] getAnnotatedClasses() { | ||
| return new Class<?>[] { Asset.class }; | ||
| } | ||
|
|
||
| @Test | ||
| @Priority(10) | ||
| public void initData() { | ||
| final Path path = Path.of( getClass().getResource( "./blob.txt" ).getPath() ); | ||
| doInJPA( this::entityManagerFactory, entityManager -> { | ||
| try { | ||
| final Asset asset = new Asset(); | ||
| asset.setFileName( "blob.txt" ); | ||
|
|
||
| final InputStream stream = new BufferedInputStream( Files.newInputStream( path ) ); | ||
Check warningCode scanning / CodeQL Potential input resource leak
This BufferedInputStream is not always closed on method exit.
|
||
| 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" ); | ||
| } | ||
| } ); | ||
| } | ||
|
|
||
| @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; | ||
| } | ||
| } | ||
|
|
||
| } | ||
33 changes: 33 additions & 0 deletions
33
hibernate-envers/src/test/java/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,33 @@ | ||
| <?xml version='1.0' encoding='utf-8'?> | ||
| <!-- | ||
| ~ Hibernate, Relational Persistence for Idiomatic Java | ||
| ~ | ||
| ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
| ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
| --> | ||
| <!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> |
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.
Check warning
Code scanning / CodeQL
Unsafe use of getResource