HHH-16857 fix the syntax for NVARCHAR literals on SQL Server

and improve an error message
This commit is contained in:
Gavin King 2023-06-27 23:32:43 +02:00
parent e76d1e5752
commit 2a3bab5e81
3 changed files with 36 additions and 8 deletions

View File

@ -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<O,R> implements JpaAttributeConverter<O,R
throw pe;
}
catch (RuntimeException re) {
throw new PersistenceException( "Error attempting to apply AttributeConverter", re );
throw new PersistenceException( "Error attempting to apply AttributeConverter: " + re.getMessage(), re );
}
}

View File

@ -19,7 +19,7 @@ import org.hibernate.type.descriptor.jdbc.spi.BasicJdbcLiteralFormatter;
* @author Steve Ebersole
*/
public class JdbcLiteralFormatterCharacterData<T> extends BasicJdbcLiteralFormatter<T> {
public static final String NATIONALIZED_PREFIX = "n";
public static final String NATIONALIZED_PREFIX = "N";
private final boolean isNationalized;

View File

@ -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<Object[]> users = session.createNativeQuery(
"select * from users" ).getResultList();
List<Object[]> users = session.createNativeQuery("select * from users" ).getResultList();
assertEquals( 2, users.size() );
} );
}
enum Role { ADMIN, USER, GUEST }
static class Converter implements AttributeConverter<Role,String> {
@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;
}
}
}