From 2a3bab5e81481b1d7816b94860c5db4a4223bef9 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Tue, 27 Jun 2023 23:32:43 +0200 Subject: [PATCH] HHH-16857 fix the syntax for NVARCHAR literals on SQL Server and improve an error message --- .../internal/JpaAttributeConverterImpl.java | 5 +-- .../JdbcLiteralFormatterCharacterData.java | 2 +- .../SQLServerNationalizedScalarQueryTest.java | 37 +++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/JpaAttributeConverterImpl.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/JpaAttributeConverterImpl.java index 45f2215540..77f7c989e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/JpaAttributeConverterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/JpaAttributeConverterImpl.java @@ -10,15 +10,12 @@ import jakarta.persistence.AttributeConverter; import jakarta.persistence.PersistenceException; import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext; -import org.hibernate.dialect.Dialect; -import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter; import org.hibernate.resource.beans.spi.ManagedBean; import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry; import org.hibernate.type.descriptor.java.spi.RegistryHelper; -import org.hibernate.type.descriptor.jdbc.JdbcType; /** * Standard implementation of {@link JpaAttributeConverter}. @@ -106,7 +103,7 @@ public class JpaAttributeConverterImpl implements JpaAttributeConverter extends BasicJdbcLiteralFormatter { - public static final String NATIONALIZED_PREFIX = "n"; + public static final String NATIONALIZED_PREFIX = "N"; private final boolean isNationalized; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/SQLServerNationalizedScalarQueryTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/SQLServerNationalizedScalarQueryTest.java index c1a04e3c48..d03d8acb52 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/SQLServerNationalizedScalarQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/SQLServerNationalizedScalarQueryTest.java @@ -8,6 +8,8 @@ package org.hibernate.orm.test.query; import java.util.List; +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Convert; import org.hibernate.annotations.Nationalized; import org.hibernate.dialect.SQLServerDialect; @@ -28,7 +30,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Vlad Mihalcea */ -@TestForIssue(jiraKey = "HHH-10183") @RequiresDialect(value = SQLServerDialect.class) @DomainModel( annotatedClasses = SQLServerNationalizedScalarQueryTest.User.class @@ -36,7 +37,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; @SessionFactory public class SQLServerNationalizedScalarQueryTest { + @TestForIssue(jiraKey = "HHH-16857") + @Test + public void testLiteral(SessionFactoryScope scope) { + scope.inTransaction(session -> session.createSelectionQuery("from User where name = 'Gavin'").getResultList()); + scope.inTransaction(session -> session.createSelectionQuery("from User where role = 'ADMIN'").getResultList()); + } + @TestForIssue(jiraKey = "HHH-10183") @Test public void testScalarResult(SessionFactoryScope scope) { @@ -49,18 +57,32 @@ public class SQLServerNationalizedScalarQueryTest { } ); scope.inTransaction( session -> { - List users = session.createNativeQuery( - "select * from users" ).getResultList(); + List users = session.createNativeQuery("select * from users" ).getResultList(); assertEquals( 2, users.size() ); } ); } + enum Role { ADMIN, USER, GUEST } + + static class Converter implements AttributeConverter { + @Override + public String convertToDatabaseColumn(Role attribute) { + return attribute==null ? null : attribute.name(); + } + + @Override + public Role convertToEntityAttribute(String name) { + return name==null ? null : Role.valueOf(name); + } + } + @Entity(name = "User") @Table(name = "users") public static class User { private Integer id; private String name; + private Role role = Role.USER; public User() { @@ -90,5 +112,14 @@ public class SQLServerNationalizedScalarQueryTest { this.name = name; } + @Nationalized + @Convert(converter = Converter.class) + public Role getRole() { + return role; + } + + public void setRole(Role role) { + this.role = role; + } } }