From 361587c8584d0977308d667795ccfdab39b9313c Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Mon, 3 Sep 2018 10:25:49 +0300 Subject: [PATCH] HHH-12943 - SQL Server UNIQUEIDENTIFIER type uses an uppercase UUID String representation --- ...GeneratorBinaryUniqueIdentifierIdTest.java | 88 +++++++++++++++++ ...GeneratorStringUniqueIdentifierIdTest.java | 94 +++++++++++++++++++ .../UUID2GeneratorUniqueIdentifierIdTest.java | 88 +++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorBinaryUniqueIdentifierIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorStringUniqueIdentifierIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorUniqueIdentifierIdTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorBinaryUniqueIdentifierIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorBinaryUniqueIdentifierIdTest.java new file mode 100644 index 0000000000..1326c2ce10 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorBinaryUniqueIdentifierIdTest.java @@ -0,0 +1,88 @@ +package org.hibernate.test.id.uuid; + +import java.util.HashSet; +import java.util.Set; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.dialect.SQLServerDialect; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect( SQLServerDialect.class ) +@TestForIssue( jiraKey = "HHH-12943" ) +public class UUID2GeneratorBinaryUniqueIdentifierIdTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { FooEntity.class }; + } + + @Test + public void testPaginationQuery() { + byte[] id = doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = new FooEntity(); + entity.getFooValues().add("one"); + entity.getFooValues().add("two"); + entity.getFooValues().add("three"); + + entityManager.persist(entity); + + return entity.getId(); + } ); + + assertNotNull(id); + + doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = entityManager.find(FooEntity.class, id); + assertNotNull(entity); + + assertTrue(entity.getFooValues().size() == 3); + } ); + } + + @Entity + @Table(name = "foo") + public static class FooEntity { + + @Id + @GenericGenerator(name = "uuid", strategy = "uuid2") + @GeneratedValue(generator = "uuid") + @Column(columnDefinition = "UNIQUEIDENTIFIER") + private byte[] id; + + @ElementCollection + @JoinTable(name = "foo_values") + @Column(name = "foo_value") + private final Set fooValues = new HashSet<>(); + + public byte[] getId() { + return id; + } + + public void setId(byte[] id) { + this.id = id; + } + + public Set getFooValues() { + return fooValues; + } + + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorStringUniqueIdentifierIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorStringUniqueIdentifierIdTest.java new file mode 100644 index 0000000000..284f3d59a2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorStringUniqueIdentifierIdTest.java @@ -0,0 +1,94 @@ +package org.hibernate.test.id.uuid; + +import java.util.HashSet; +import java.util.Set; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.dialect.SQLServerDialect; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect( SQLServerDialect.class ) +@TestForIssue( jiraKey = "HHH-12943" ) +public class UUID2GeneratorStringUniqueIdentifierIdTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { FooEntity.class }; + } + + @Test + public void testPaginationQuery() { + String id = doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = new FooEntity(); + entity.getFooValues().add("one"); + entity.getFooValues().add("two"); + entity.getFooValues().add("three"); + + entityManager.persist(entity); + + return entity.getId(); + } ); + + assertNotNull(id); + + doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = entityManager.find(FooEntity.class, id.toUpperCase()); + assertNotNull(entity); + + assertTrue(entity.getFooValues().size() == 3); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = entityManager.find(FooEntity.class, id); + assertNotNull(entity); + + assertTrue(entity.getFooValues().size() == 3); + } ); + } + + @Entity + @Table(name = "foo") + public static class FooEntity { + + @Id + @GenericGenerator(name = "uuid", strategy = "uuid2") + @GeneratedValue(generator = "uuid") + @Column(columnDefinition = "UNIQUEIDENTIFIER") + private String id; + + @ElementCollection + @JoinTable(name = "foo_values") + @Column(name = "foo_value") + private final Set fooValues = new HashSet<>(); + + public String getId() { + return id.toUpperCase(); + } + + public void setId(String id) { + this.id = id; + } + + public Set getFooValues() { + return fooValues; + } + + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorUniqueIdentifierIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorUniqueIdentifierIdTest.java new file mode 100644 index 0000000000..b92b013513 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/uuid/UUID2GeneratorUniqueIdentifierIdTest.java @@ -0,0 +1,88 @@ +package org.hibernate.test.id.uuid; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.dialect.SQLServerDialect; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect( SQLServerDialect.class ) +@TestForIssue( jiraKey = "HHH-12943" ) +public class UUID2GeneratorUniqueIdentifierIdTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { FooEntity.class }; + } + + @Test + public void testPaginationQuery() { + UUID id = doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = new FooEntity(); + entity.getFooValues().add("one"); + entity.getFooValues().add("two"); + entity.getFooValues().add("three"); + + entityManager.persist(entity); + + return entity.getId(); + } ); + + assertNotNull(id); + + doInJPA( this::entityManagerFactory, entityManager -> { + FooEntity entity = entityManager.find(FooEntity.class, id); + assertNotNull(entity); + + assertTrue(entity.getFooValues().size() == 3); + } ); + } + + @Entity + @Table(name = "foo") + public static class FooEntity { + + @Id + @GenericGenerator(name = "uuid", strategy = "uuid2") + @GeneratedValue(generator = "uuid") + @Column(columnDefinition = "UNIQUEIDENTIFIER") + private UUID id; + + @ElementCollection + @JoinTable(name = "foo_values") + @Column(name = "foo_value") + private final Set fooValues = new HashSet<>(); + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public Set getFooValues() { + return fooValues; + } + + } +} \ No newline at end of file