diff --git a/documentation/src/test/java/org/hibernate/userguide/mapping/identifier/UuidGeneratedValueTest.java b/documentation/src/test/java/org/hibernate/userguide/mapping/identifier/UuidGeneratedValueTest.java index 3df928abb1..ce948c6aa6 100644 --- a/documentation/src/test/java/org/hibernate/userguide/mapping/identifier/UuidGeneratedValueTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/mapping/identifier/UuidGeneratedValueTest.java @@ -17,6 +17,7 @@ import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; /** @@ -43,6 +44,11 @@ public class UuidGeneratedValueTest extends BaseEntityManagerFunctionalTestCase }); assertNotNull(book.getId()); + + doInJPA(this::entityManagerFactory, entityManager -> { + Book foundBook = entityManager.find( Book.class, book.getId() ); + assertEquals( book.getAuthor(), foundBook.getAuthor() ); + }); } //tag::identifiers-generators-uuid-mapping-example[] diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java index 8db89b8926..67b7f60676 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java @@ -20,7 +20,6 @@ import org.hibernate.dialect.identity.SQLServerIdentityColumnSupport; import org.hibernate.dialect.pagination.LimitHandler; import org.hibernate.dialect.pagination.SQLServer2005LimitHandler; import org.hibernate.dialect.pagination.SQLServer2012LimitHandler; -import org.hibernate.dialect.pagination.TopLimitHandler; import org.hibernate.dialect.sequence.NoSequenceSupport; import org.hibernate.dialect.sequence.SQLServer16SequenceSupport; import org.hibernate.dialect.sequence.SQLServerSequenceSupport; @@ -53,8 +52,11 @@ import org.hibernate.type.BasicType; import org.hibernate.type.BasicTypeRegistry; import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.descriptor.java.PrimitiveByteArrayJavaType; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.SmallIntJdbcType; +import org.hibernate.type.descriptor.jdbc.UUIDJdbcType; import org.hibernate.type.descriptor.jdbc.XmlJdbcType; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl; import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry; @@ -174,6 +176,26 @@ public class SQLServerDialect extends AbstractTransactSQLDialect { ddlTypeRegistry.addDescriptor( new DdlTypeImpl( GEOMETRY, "geometry", this ) ); ddlTypeRegistry.addDescriptor( new DdlTypeImpl( GEOGRAPHY, "geography", this ) ); ddlTypeRegistry.addDescriptor( new DdlTypeImpl( SQLXML, "xml", this ) ); + ddlTypeRegistry.addDescriptor( new DdlTypeImpl( UUID, "uniqueidentifier", this ) ); + } + + @Override + public JdbcType resolveSqlTypeDescriptor( + String columnTypeName, + int jdbcTypeCode, + int precision, + int scale, + JdbcTypeRegistry jdbcTypeRegistry) { + switch ( jdbcTypeCode ) { + case OTHER: + switch ( columnTypeName ) { + case "uniqueidentifier": + jdbcTypeCode = UUID; + break; + } + break; + } + return super.resolveSqlTypeDescriptor( columnTypeName, jdbcTypeCode, precision, scale, jdbcTypeRegistry ); } @Override @@ -213,6 +235,7 @@ public class SQLServerDialect extends AbstractTransactSQLDialect { SmallIntJdbcType.INSTANCE ); typeContributions.contributeJdbcType( XmlJdbcType.INSTANCE ); + typeContributions.contributeJdbcType( UUIDJdbcType.INSTANCE ); } @Override diff --git a/migration-guide.adoc b/migration-guide.adoc index 6aab874ff5..d66e4b28d7 100644 --- a/migration-guide.adoc +++ b/migration-guide.adoc @@ -22,3 +22,12 @@ Due to this change, schema validation errors could occur on existing databases. The migration to `uuid` requires a migration expression like `cast(old as uuid)`. To retain backwards compatibility, configure the setting `hibernate.type.preferred_uuid_jdbc_type` to `BINARY`. + +=== UUID mapping changes on SQL Server + +On SQL Server, the type code `SqlType.UUID` now by default refers to the DDL type `uniqueidentifier`, whereas before it was using `binary(16)`. +Due to this change, schema validation errors could occur on existing databases. + +The migration to `uuid` requires a migration expression like `cast(old as uuid)`. + +To retain backwards compatibility, configure the setting `hibernate.type.preferred_uuid_jdbc_type` to `BINARY`.