From dd370313f2ca420638c829e4f94919a953fc8a47 Mon Sep 17 00:00:00 2001 From: Gavin Date: Tue, 11 Apr 2023 16:23:20 +0200 Subject: [PATCH] add test showing how to use gen_random_uuid() on postgres --- .../sqldefault/PostgresUuidTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/sqldefault/PostgresUuidTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/sqldefault/PostgresUuidTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/sqldefault/PostgresUuidTest.java new file mode 100644 index 0000000000..c9ef53cd2b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/sqldefault/PostgresUuidTest.java @@ -0,0 +1,38 @@ +package org.hibernate.orm.test.mapping.generated.sqldefault; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.Generated; +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +@SessionFactory +@DomainModel(annotatedClasses = PostgresUuidTest.It.class) +@RequiresDialect(PostgreSQLDialect.class) +public class PostgresUuidTest { + + @Test + void testit(SessionFactoryScope scope) { + scope.inTransaction( s -> s.persist( new It() ) ); + scope.inTransaction( s -> { + UUID uuid = s.createQuery("select uuid from It", UUID.class).getSingleResult(); + String string = s.createQuery("select str(uuid) from It", String.class).getSingleResult(); + Assertions.assertEquals( string, uuid.toString() ); + }); + } + + @Entity(name="It") + public static class It { + @Id @Generated + @ColumnDefault("gen_random_uuid()") + UUID uuid; + } +}