From f3f154b89a4881250072e00b7301cf0054349c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Tue, 6 Aug 2024 15:54:45 +0200 Subject: [PATCH 1/2] HHH-18446 Test case --- .../hibernate/orm/test/type/LongNullTest.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/LongNullTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/LongNullTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/LongNullTest.java new file mode 100644 index 000000000000..018dfaffe996 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/LongNullTest.java @@ -0,0 +1,72 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.type; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.type.SqlTypes; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DomainModel(annotatedClasses = LongNullTest.Foo.class) +@SessionFactory +public class LongNullTest { + + @Entity + public static final class Foo { + @Id + @GeneratedValue + private Long id; + + @JdbcTypeCode(SqlTypes.LONG32NVARCHAR) + @Column + private String field = null; + + @JdbcTypeCode(SqlTypes.LONG32VARCHAR) + @Column + private String nfield = null; + + @JdbcTypeCode(SqlTypes.LONG32VARBINARY) + @Column + private byte[] bfield = null; + } + + @Test + public void testNull(SessionFactoryScope scope) { + final var expected = scope.fromTransaction( s -> { + final var foo = new Foo(); + s.persist( foo ); + return foo; + } ); + final var actual = scope.fromSession( s -> s.find( Foo.class, expected.id ) ); + assertEquals( expected.field, actual.field ); + assertEquals( expected.nfield, actual.nfield ); + assertArrayEquals( expected.bfield, actual.bfield ); + } + + @Test + public void testNonNull(SessionFactoryScope scope) { + final var expected = scope.fromTransaction( s -> { + final var foo = new Foo(); + foo.bfield = "ABC".getBytes(); + foo.field = "DEF"; + foo.nfield = "GHI"; + s.persist( foo ); + return foo; + } ); + final var actual = scope.fromSession( s -> s.find( Foo.class, expected.id ) ); + assertEquals( expected.field, actual.field ); + assertEquals( expected.nfield, actual.nfield ); + assertArrayEquals( expected.bfield, actual.bfield ); + } +} From 60626228efe9bc924c3ea37012b8fe1a22ad4686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Tue, 6 Aug 2024 15:54:55 +0200 Subject: [PATCH 2/2] HHH-18446 Added default SQL type code to LongVarbinaryJdbcType abd LongVarcharJdbcType --- .../jdbc/LongVarbinaryJdbcType.java | 15 ++++++++++----- .../descriptor/jdbc/LongVarcharJdbcType.java | 19 ++++++++++++------- .../jdbc/internal/JdbcTypeBaseline.java | 6 +++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarbinaryJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarbinaryJdbcType.java index b147796c6ad3..d8863444156f 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarbinaryJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarbinaryJdbcType.java @@ -14,14 +14,19 @@ public class LongVarbinaryJdbcType extends VarbinaryJdbcType { public static final LongVarbinaryJdbcType INSTANCE = new LongVarbinaryJdbcType(); - private final int jdbcTypeCode; + private final int defaultSqlTypeCode; public LongVarbinaryJdbcType() { - this(Types.LONGVARBINARY); + this( Types.LONGVARBINARY ); } - public LongVarbinaryJdbcType(int jdbcTypeCode) { - this.jdbcTypeCode = jdbcTypeCode; + public LongVarbinaryJdbcType(int defaultSqlTypeCode) { + this.defaultSqlTypeCode = defaultSqlTypeCode; + } + + @Override + public int getDefaultSqlTypeCode() { + return defaultSqlTypeCode; } @Override @@ -31,6 +36,6 @@ public String toString() { @Override public int getJdbcTypeCode() { - return jdbcTypeCode; + return Types.LONGVARBINARY; } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarcharJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarcharJdbcType.java index 5740bf3731f8..f882b5aadaf3 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarcharJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/LongVarcharJdbcType.java @@ -4,13 +4,13 @@ */ package org.hibernate.type.descriptor.jdbc; -import java.sql.Types; - import org.hibernate.type.SqlTypes; import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.type.spi.TypeConfiguration; +import java.sql.Types; + /** * Descriptor for {@link Types#LONGVARCHAR LONGVARCHAR} handling. * @@ -19,14 +19,14 @@ public class LongVarcharJdbcType extends VarcharJdbcType { public static final LongVarcharJdbcType INSTANCE = new LongVarcharJdbcType(); - private final int jdbcTypeCode; + private final int defaultSqlTypeCode; public LongVarcharJdbcType() { - this(Types.LONGVARCHAR); + this( Types.LONGVARCHAR ); } - public LongVarcharJdbcType(int jdbcTypeCode) { - this.jdbcTypeCode = jdbcTypeCode; + public LongVarcharJdbcType(int defaultSqlTypeCode) { + this.defaultSqlTypeCode = defaultSqlTypeCode; } @Override @@ -36,7 +36,12 @@ public String toString() { @Override public int getJdbcTypeCode() { - return jdbcTypeCode; + return Types.LONGVARCHAR; + } + + @Override + public int getDefaultSqlTypeCode() { + return defaultSqlTypeCode; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java index 3cba38590b03..e8d1917629e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java @@ -85,12 +85,12 @@ public static void prime(BaselineTarget target) { target.addDescriptor( BinaryJdbcType.INSTANCE ); target.addDescriptor( VarbinaryJdbcType.INSTANCE ); target.addDescriptor( LongVarbinaryJdbcType.INSTANCE ); - target.addDescriptor( new LongVarbinaryJdbcType(SqlTypes.LONG32VARBINARY) ); + target.addDescriptor( new LongVarbinaryJdbcType( SqlTypes.LONG32VARBINARY) ); target.addDescriptor( CharJdbcType.INSTANCE ); target.addDescriptor( VarcharJdbcType.INSTANCE ); target.addDescriptor( LongVarcharJdbcType.INSTANCE ); - target.addDescriptor( new LongVarcharJdbcType(SqlTypes.LONG32VARCHAR) ); + target.addDescriptor( new LongVarcharJdbcType( SqlTypes.LONG32VARCHAR) ); target.addDescriptor( BlobJdbcType.DEFAULT ); target.addDescriptor( ClobJdbcType.DEFAULT ); @@ -101,7 +101,7 @@ public static void prime(BaselineTarget target) { target.addDescriptor( Types.NVARCHAR, VarcharJdbcType.INSTANCE ); target.addDescriptor( Types.LONGNVARCHAR, LongVarcharJdbcType.INSTANCE ); target.addDescriptor( Types.NCLOB, ClobJdbcType.DEFAULT ); - target.addDescriptor( new LongVarcharJdbcType(SqlTypes.LONG32NVARCHAR) ); + target.addDescriptor( new LongVarcharJdbcType( SqlTypes.LONG32NVARCHAR) ); target.addDescriptor( RowIdJdbcType.INSTANCE ); }