diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java index aba4690930..7e21bb53f7 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java @@ -218,9 +218,7 @@ public class OracleArrayJdbcType implements JdbcType { } String[] getDropArrayTypeCommand(String elementTypeName) { - // for some weird reason dropping the type declarations causes problem in the test suite -// return new String[] { "drop type " + elementTypeName }; - return EMPTY_STRING_ARRAY; + return EMPTY_STRING_ARRAY; //new String[] { "drop type " + elementTypeName + " force" }; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleNestedTableJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleNestedTableJdbcType.java index da0457a46a..8460d36267 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleNestedTableJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleNestedTableJdbcType.java @@ -225,9 +225,7 @@ public class OracleNestedTableJdbcType implements JdbcType { } String[] getDropArrayTypeCommand(String elementTypeName) { - // for some weird reason dropping the type declarations causes problem in the test suite -// return new String[] { "drop type " + elementTypeName }; - return EMPTY_STRING_ARRAY; + return EMPTY_STRING_ARRAY; //new String[] { "drop type " + elementTypeName + " force" }; } @Override 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 index 45a3e5970d..42f6b69052 100644 --- 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 @@ -40,32 +40,60 @@ public class OracleNestedTableTest { @Test public void testSchema(SessionFactoryScope scope) { scope.inSession( s -> { - try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) { + Connection c; + try { + c = s.getJdbcConnectionAccess().obtainConnection(); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + try { 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"); + fail("nested table column not exported"); } catch (SQLException e) { throw new RuntimeException(e); } + finally { + try { + s.getJdbcConnectionAccess().releaseConnection(c); + } + catch (SQLException e) { + } + } }); scope.inSession( s -> { - try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) { + Connection c; + try { + c = s.getJdbcConnectionAccess().obtainConnection(); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + try { 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"); + fail("nested table column not exported"); } catch (SQLException e) { throw new RuntimeException(e); } + finally { + try { + s.getJdbcConnectionAccess().releaseConnection(c); + } + catch (SQLException e) { + } + } }); } @@ -78,10 +106,11 @@ public class OracleNestedTableTest { @Array(length = 33) @Column(length = 25) - @JdbcTypeCode(SqlTypes.ARRAY) + @JdbcTypeCode(SqlTypes.TABLE) String[] strings; @Array(length = 2) + @JdbcTypeCode(SqlTypes.TABLE) ActivityType[] activityTypes; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleSqlArrayTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleSqlArrayTest.java new file mode 100644 index 0000000000..8ece85cfa3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleSqlArrayTest.java @@ -0,0 +1,119 @@ +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.math.BigInteger; +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 = {OracleSqlArrayTest.Container.class}) +@RequiresDialect(OracleDialect.class) +public class OracleSqlArrayTest { + + @Test public void test(SessionFactoryScope scope) { + Container container = new Container(); + container.activityKinds = new ActivityKind[] { ActivityKind.Work, ActivityKind.Play }; + container.bigIntegers = new BigInteger[] { new BigInteger("123"), new BigInteger("345") }; + scope.inTransaction( s -> s.persist( container ) ); + Container c = scope.fromTransaction( s-> s.createQuery("from ContainerWithArrays where bigIntegers = ?1", Container.class ).setParameter(1, new BigInteger[] { new BigInteger("123"), new BigInteger("345") }).getSingleResult() ); + assertArrayEquals( c.activityKinds, new ActivityKind[] { ActivityKind.Work, ActivityKind.Play } ); + assertArrayEquals( c.bigIntegers, new BigInteger[] { new BigInteger("123"), new BigInteger("345") } ); + c = scope.fromTransaction( s-> s.createQuery("from ContainerWithArrays where activityKinds = ?1", Container.class ).setParameter(1, new ActivityKind[] { ActivityKind.Work, ActivityKind.Play }).getSingleResult() ); + } + + @Test public void testSchema(SessionFactoryScope scope) { + scope.inSession( s -> { + Connection c; + try { + c = s.getJdbcConnectionAccess().obtainConnection(); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + try { + ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "BIGINTEGERS" ); + while ( tableInfo.next() ) { + String type = tableInfo.getString(6); + assertEquals( "BIGINTEGERARRAY", type ); + return; + } + fail("named array column not exported"); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + finally { + try { + s.getJdbcConnectionAccess().releaseConnection(c); + } + catch (SQLException e) { + } + } + }); + scope.inSession( s -> { + Connection c; + try { + c = s.getJdbcConnectionAccess().obtainConnection(); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + try { + ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "ACTIVITYKINDS" ); + while ( tableInfo.next() ) { + String type = tableInfo.getString(6); + assertEquals( "ACTIVITYKINDARRAY", type ); + return; + } + fail("named array column not exported"); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + finally { + try { + s.getJdbcConnectionAccess().releaseConnection(c); + } + catch (SQLException e) { + } + } + }); + } + + public enum ActivityKind { Work, Play, Sleep } + + @Entity(name = "ContainerWithArrays") + public static class Container { + + @Id @GeneratedValue Long id; + + @Array(length = 33) + @Column(length = 25) + @JdbcTypeCode(SqlTypes.ARRAY) + BigInteger[] bigIntegers; + + @Array(length = 2) + @JdbcTypeCode(SqlTypes.ARRAY) + ActivityKind[] activityKinds; + + } + +}