Skip to content
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

HHH-18446 Cannot use @JdbcTypeCode(SqlTypes.LONG32VARBINARY) on field with null value #9103

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +36,6 @@ public String toString() {

@Override
public int getJdbcTypeCode() {
return jdbcTypeCode;
return Types.LONGVARBINARY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand All @@ -36,7 +36,12 @@ public String toString() {

@Override
public int getJdbcTypeCode() {
return jdbcTypeCode;
return Types.LONGVARCHAR;
}

@Override
public int getDefaultSqlTypeCode() {
return defaultSqlTypeCode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
}
}
Loading