diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNestedTableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNestedTableTest.java new file mode 100644 index 0000000000..45a3e5970d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNestedTableTest.java @@ -0,0 +1,89 @@ +package org.hibernate.orm.test.type; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import org.hibernate.annotations.Array; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.dialect.OracleDialect; +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.hibernate.type.SqlTypes; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +@SessionFactory +@DomainModel(annotatedClasses = {OracleNestedTableTest.Container.class}) +@RequiresDialect(OracleDialect.class) +public class OracleNestedTableTest { + + @Test public void test(SessionFactoryScope scope) { + Container container = new Container(); + container.activityTypes = new ActivityType[] { ActivityType.Work, ActivityType.Play }; + container.strings = new String[] { "hello", "world" }; + scope.inTransaction( s -> s.persist( container ) ); + Container c = scope.fromTransaction( s-> s.createQuery("from ContainerWithArrays where strings = ?1", Container.class ).setParameter(1, new String[] { "hello", "world" }).getSingleResult() ); + assertArrayEquals( c.activityTypes, new ActivityType[] { ActivityType.Work, ActivityType.Play } ); + assertArrayEquals( c.strings, new String[] { "hello", "world" } ); + c = scope.fromTransaction( s-> s.createQuery("from ContainerWithArrays where activityTypes = ?1", Container.class ).setParameter(1, new ActivityType[] { ActivityType.Work, ActivityType.Play }).getSingleResult() ); + } + + @Test public void testSchema(SessionFactoryScope scope) { + scope.inSession( s -> { + try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) { + ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "STRINGS" ); + while ( tableInfo.next() ) { + String type = tableInfo.getString(6); + assertEquals( "STRINGARRAY", type ); + return; + } + fail("named enum column not exported"); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + }); + scope.inSession( s -> { + try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) { + ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "ACTIVITYTYPES" ); + while ( tableInfo.next() ) { + String type = tableInfo.getString(6); + assertEquals( "ACTIVITYTYPEARRAY", type ); + return; + } + fail("named enum column not exported"); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + }); + } + + public enum ActivityType { Work, Play, Sleep } + + @Entity(name = "ContainerWithArrays") + public static class Container { + + @Id @GeneratedValue Long id; + + @Array(length = 33) + @Column(length = 25) + @JdbcTypeCode(SqlTypes.ARRAY) + String[] strings; + + @Array(length = 2) + ActivityType[] activityTypes; + + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java new file mode 100644 index 0000000000..ad94419821 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java @@ -0,0 +1,94 @@ +package org.hibernate.orm.test.type; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import org.hibernate.annotations.JdbcTypeCode; +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.hibernate.type.SqlTypes; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +@SessionFactory +@DomainModel(annotatedClasses = {PostgresEnumTest.Timeslot.class, PostgresEnumTest.Activity.class}) +@RequiresDialect(PostgreSQLDialect.class) +public class PostgresEnumTest { + + @Test public void test(SessionFactoryScope scope) { + Timeslot timeslot = new Timeslot(); + Activity activity = new Activity(); + activity.activityType = ActivityType.Play; + timeslot.activity = activity; + scope.inTransaction( s -> s.persist( timeslot ) ); + Timeslot ts = scope.fromTransaction( s-> s.createQuery("from Timeslot where activity.activityType = Play", Timeslot.class ).getSingleResult() ); + assertEquals( ts.activity.activityType, ActivityType.Play ); + } + + @Test public void testSchema(SessionFactoryScope scope) { + scope.inSession( s -> { + try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) { + ResultSet typeInfo = c.getMetaData().getTypeInfo(); + while ( typeInfo.next() ) { + String name = typeInfo.getString(1); + if ( name.equalsIgnoreCase("ActivityType") ) { + return; + } + } + fail("named enum type not exported"); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + }); + scope.inSession( s -> { + try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) { + ResultSet tableInfo = c.getMetaData().getColumns(null, null, "activity", "activitytype" ); + while ( tableInfo.next() ) { + String type = tableInfo.getString(6); + assertEquals( "activitytype", type ); + return; + } + fail("named enum column not exported"); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + }); + } + + public enum ActivityType {Work, Play, Sleep } + + @Entity(name = "Activity") + public static class Activity { + + @Id + @JdbcTypeCode(SqlTypes.NAMED_ENUM) + ActivityType activityType; + + } + + @Entity(name = "Timeslot") + public static class Timeslot { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int id; + + @ManyToOne(cascade = CascadeType.PERSIST) + Activity activity; + } +}