====== Hibernate & MS SQL Server ====== * [[java:hibernate|Hibernate]]와 [[:mssqlserver|MS SQL Server]] ===== NVARCHAR Dialect 매핑 ===== * Hibernate가 MS SQLServer의 ''NVARCHAR''를 인식하지 못하는 문제 발생시 아래 오류 발생 org.hibernate.MappingException: No Dialect mapping for JDBC type: -9 * [[https://medium.com/@SlackBeck/ms-sql-server%EC%97%90%EC%84%9C-jpa-nativequery-%EC%82%AC%EC%9A%A9%EC%8B%9C-%EC%9C%A0%EB%8B%88%EC%BD%94%EB%93%9C-unicode-%EB%AC%B8%EC%A0%9C-eabc1d912c04|MS SQL Server에서 JPA NativeQuery 사용시 유니코드(Unicode) 문제]] * [[https://stackoverflow.com/questions/27039300/jpa-sql-server-no-dialect-mapping-for-jdbc-type-9|java - JPA SQL Server No Dialect mapping for JDBC type: -9]] * [[https://questionfocus.com/getting-hibernate-and-sql-server-to-play-nice-with-varchar-and-nvarchar.html|Getting Hibernate and SQL Server to play nice with VARCHAR and NVARCHAR - QuestionFocus]] * [[https://blogs.msdn.microsoft.com/sqlcat/2010/04/05/character-data-type-conversion-when-using-sql-server-jdbc-drivers/|Character data type conversion when using SQL Server JDBC drivers | SQL Server Customer Advisory Team]] ==== Entity에 NVARCHAR 컬럼 선언을 하려면 ==== * [[https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/SQLServer2008Dialect.java|SQLServer2008Dialect]]에서 NVARCHAR ''registerColumnType'' 돼 있음 따라서 이 이후 버전 Dialect를 사용하면 NVARCHAR 매핑이 작동한다. * ''@org.hibernate.annotations.Nationalized'' 애노테이션을 통해 ''NVARCHAR'' 로 Entity Column 매핑을 할 수 있다. // NVARCHAR 매핑 @Basic @Nationalized public String description; // NCLOB 매핑 @Lob @Basic @Nationalized public NClob description; ==== SELECT 결과 컬럼에 NVARCHAR 등이 있을 경우에 해석을 위한 Dialect 추가 설정 ==== package com.nhl.dao; import java.sql.Types; import org.hibernate.dialect.SQLServerDialect; import org.hibernate.type.StandardBasicTypes; public class SQlServerDBDialect extends SQLServerDialect { public SQlServerDBDialect() { super(); registerHibernateType(Types.NCHAR, StandardBasicTypes.CHARACTER.getName()); registerHibernateType(Types.NCHAR, 1, StandardBasicTypes.CHARACTER.getName()); registerHibernateType(Types.NCHAR, 255, StandardBasicTypes.STRING.getName()); registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName()); registerHibernateType(Types.LONGNVARCHAR, StandardBasicTypes.TEXT.getName()); registerHibernateType(Types.NCLOB, StandardBasicTypes.CLOB.getName()); } } * ''registerHibernateType''은 [[java:hibernate:dialect|Hibernate Dialect]] 참조