-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-17246 - Guard against Sybase being configured for truncating trai…
…ling zeros Signed-off-by: Jan Schatteman <[email protected]>
- Loading branch information
Showing
9 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
27 changes: 27 additions & 0 deletions
27
hibernate-core/src/main/java/org/hibernate/type/descriptor/java/OracleUUIDJavaType.java
This file contains 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,27 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.type.descriptor.java; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
public class OracleUUIDJavaType extends UUIDJavaType { | ||
|
||
/* This class is related to the changes that were made for HHH-17246 */ | ||
|
||
public static final OracleUUIDJavaType INSTANCE = new OracleUUIDJavaType(); | ||
|
||
@Override | ||
public String toString(UUID value) { | ||
return NoDashesStringTransformer.INSTANCE.transform( value ); | ||
} | ||
|
||
@Override | ||
public UUID fromString(CharSequence string) { | ||
return NoDashesStringTransformer.INSTANCE.parse( string.toString() ); | ||
} | ||
} |
This file contains 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
60 changes: 60 additions & 0 deletions
60
hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/UuidAsBinaryJdbcType.java
This file contains 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,60 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.type.descriptor.jdbc; | ||
|
||
import org.hibernate.type.descriptor.ValueExtractor; | ||
import org.hibernate.type.descriptor.WrapperOptions; | ||
import org.hibernate.type.descriptor.java.JavaType; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.Arrays; | ||
|
||
import static org.hibernate.type.SqlTypes.UUID; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
public class UuidAsBinaryJdbcType extends BinaryJdbcType { | ||
|
||
public static final UuidAsBinaryJdbcType INSTANCE = new UuidAsBinaryJdbcType(); | ||
|
||
@Override | ||
public int getDdlTypeCode() { | ||
return Types.BINARY; | ||
} | ||
|
||
@Override | ||
public int getDefaultSqlTypeCode() { | ||
return UUID; | ||
} | ||
|
||
@Override | ||
public <X> ValueExtractor<X> getExtractor( JavaType<X> javaType ) { | ||
return new BasicExtractor<>( javaType, this ) { | ||
@Override | ||
protected X doExtract( ResultSet rs, int paramIndex, WrapperOptions options ) throws SQLException { | ||
final byte[] bytes = rs.getBytes( paramIndex ); | ||
return javaType.wrap( bytes == null || bytes.length == 16 ? bytes : Arrays.copyOf( bytes, 16 ), options ); | ||
} | ||
|
||
@Override | ||
protected X doExtract( CallableStatement statement, int index, WrapperOptions options ) throws SQLException { | ||
final byte[] bytes = statement.getBytes( index ); | ||
return javaType.wrap( bytes == null || bytes.length == 16 ? bytes : Arrays.copyOf( bytes, 16 ), options ); | ||
} | ||
|
||
@Override | ||
protected X doExtract( CallableStatement statement, String name, WrapperOptions options ) | ||
throws SQLException { | ||
final byte[] bytes = statement.getBytes( name ); | ||
return javaType.wrap( bytes == null || bytes.length == 16 ? bytes : Arrays.copyOf( bytes, 16 ), options ); | ||
} | ||
}; | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
hibernate-core/src/test/java/org/hibernate/orm/test/id/uuid/SybaseASEUUIDTest.java
This file contains 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,81 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.id.uuid; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import org.hibernate.annotations.JdbcType; | ||
import org.hibernate.dialect.SybaseASEDialect; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.RequiresDialect; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
@RequiresDialect(value = SybaseASEDialect.class) | ||
@DomainModel(annotatedClasses = { SybaseASEUUIDTest.Book.class }) | ||
@SessionFactory | ||
public class SybaseASEUUIDTest { | ||
|
||
private static final UUID uuid = UUID.fromString("53886a8a-7082-4879-b430-25cb94415b00"); | ||
|
||
@BeforeEach | ||
void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final Book book = new Book(uuid, "John Doe"); | ||
session.persist( book ); | ||
} ); | ||
} | ||
|
||
@AfterEach | ||
void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> session.createMutationQuery( "delete from Book" ).executeUpdate() | ||
); | ||
} | ||
|
||
@Test | ||
@JiraKey( value = "HHH-17246" ) | ||
public void testTrailingZeroByteTruncation(SessionFactoryScope scope) { | ||
scope.inSession( | ||
session -> assertEquals( 15, session.createNativeQuery("select id from Book", byte[].class).getSingleResult().length ) | ||
); | ||
scope.inTransaction( | ||
session -> { | ||
Book b = session.createQuery( "from Book", Book.class ).getSingleResult(); | ||
assertEquals(uuid, b.id); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Book") | ||
static class Book { | ||
@Id | ||
// The purpose is to effectively provoke the trailing 0 bytes truncation | ||
@JdbcType( SybaseUuidAsVarbinaryJdbcType.class ) | ||
UUID id; | ||
|
||
String author; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(UUID id, String author) { | ||
this.id = id; | ||
this.author = author; | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...nate-core/src/test/java/org/hibernate/orm/test/id/uuid/SybaseUuidAsVarbinaryJdbcType.java
This file contains 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,18 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.id.uuid; | ||
|
||
import org.hibernate.type.descriptor.jdbc.UuidAsBinaryJdbcType; | ||
import java.sql.Types; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
public class SybaseUuidAsVarbinaryJdbcType extends UuidAsBinaryJdbcType { | ||
@Override | ||
public int getDdlTypeCode() { | ||
return Types.VARBINARY; | ||
} | ||
} |