|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.id.sequence; |
| 8 | + |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.sql.Statement; |
| 12 | + |
| 13 | +import org.hibernate.SessionFactory; |
| 14 | +import org.hibernate.boot.MetadataSources; |
| 15 | +import org.hibernate.boot.model.naming.Identifier; |
| 16 | +import org.hibernate.boot.model.naming.PhysicalNamingStrategy; |
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 18 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 19 | +import org.hibernate.cfg.AvailableSettings; |
| 20 | +import org.hibernate.cfg.Environment; |
| 21 | +import org.hibernate.dialect.H2Dialect; |
| 22 | +import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; |
| 23 | +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; |
| 24 | +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; |
| 25 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 26 | +import org.hibernate.internal.util.PropertiesHelper; |
| 27 | + |
| 28 | +import org.hibernate.testing.orm.junit.BaseUnitTest; |
| 29 | +import org.hibernate.testing.orm.junit.Jira; |
| 30 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 31 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 32 | +import org.junit.jupiter.api.AfterAll; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import jakarta.persistence.Entity; |
| 37 | +import jakarta.persistence.GeneratedValue; |
| 38 | +import jakarta.persistence.GenerationType; |
| 39 | +import jakarta.persistence.Id; |
| 40 | +import jakarta.persistence.SequenceGenerator; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | +import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction; |
| 44 | +import static org.junit.jupiter.api.Assertions.fail; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author Marco Belladelli |
| 48 | + */ |
| 49 | +@BaseUnitTest |
| 50 | +@RequiresDialect( H2Dialect.class ) |
| 51 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18337" ) |
| 52 | +public class PooledWithCustomNamingStrategyTest { |
| 53 | + @Test |
| 54 | + public void testWrongIncrementSize() { |
| 55 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 56 | + registryBuilder.applySetting( AvailableSettings.PHYSICAL_NAMING_STRATEGY, MyPhysicalNamingStrategy.INSTANCE ); |
| 57 | + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( registryBuilder.build() ) |
| 58 | + .addAnnotatedClass( WrongEntity.class ) |
| 59 | + .buildMetadata(); |
| 60 | + try (final SessionFactory sf = metadata.buildSessionFactory()) { |
| 61 | + fail( "Default increment size of [50] should not work with the database sequence increment size [1]." ); |
| 62 | + } |
| 63 | + catch (Exception e) { |
| 64 | + assertThat( e.getCause().getMessage() ).isEqualTo( |
| 65 | + "The increment size of the [MY_SEQ] sequence is set to [50] in the entity " + |
| 66 | + "mapping while the associated database sequence increment size is [1]." |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testRightIncrementSize() { |
| 73 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 74 | + registryBuilder.applySetting( AvailableSettings.PHYSICAL_NAMING_STRATEGY, MyPhysicalNamingStrategy.INSTANCE ); |
| 75 | + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( registryBuilder.build() ) |
| 76 | + .addAnnotatedClass( RightEntity.class ) |
| 77 | + .buildMetadata(); |
| 78 | + try (final SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory()) { |
| 79 | + // session factory should be created correctly |
| 80 | + inTransaction( sf, session -> session.persist( new RightEntity() ) ); |
| 81 | + inTransaction( sf, session -> { |
| 82 | + final RightEntity result = session.createQuery( "from RightEntity", RightEntity.class ) |
| 83 | + .getSingleResult(); |
| 84 | + assertThat( result.id ).isNotNull(); |
| 85 | + } ); |
| 86 | + } |
| 87 | + catch (Exception e) { |
| 88 | + fail( "Expected configured increment size of [1] to be compatible with the existing sequence" ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + ConnectionProvider connectionProvider; |
| 93 | + |
| 94 | + @BeforeAll |
| 95 | + public void setUp() { |
| 96 | + final DriverManagerConnectionProviderImpl provider = new DriverManagerConnectionProviderImpl(); |
| 97 | + provider.configure( PropertiesHelper.map( Environment.getProperties() ) ); |
| 98 | + connectionProvider = provider; |
| 99 | + try (final Connection connection = connectionProvider.getConnection(); |
| 100 | + final Statement statement = connection.createStatement()) { |
| 101 | + statement.execute( "create sequence MY_SEQ start with 1 increment by 1" ); |
| 102 | + statement.execute( "create table RightEntity(id bigint not null, primary key (id))" ); |
| 103 | + } |
| 104 | + catch (SQLException e) { |
| 105 | + throw new RuntimeException( "Failed to setup the test", e ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @AfterAll |
| 110 | + public void tearDown() { |
| 111 | + try (final Connection connection = connectionProvider.getConnection(); |
| 112 | + final Statement statement = connection.createStatement()) { |
| 113 | + statement.execute( "drop sequence MY_SEQ" ); |
| 114 | + statement.execute( "drop table RightEntity" ); |
| 115 | + } |
| 116 | + catch (SQLException e) { |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Entity( name = "WrongEntity" ) |
| 121 | + static class WrongEntity { |
| 122 | + @Id |
| 123 | + @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "my-sequence-generator" ) |
| 124 | + @SequenceGenerator( name = "my-sequence-generator", sequenceName = "REPLACE_SEQ" ) |
| 125 | + private Long id; |
| 126 | + } |
| 127 | + |
| 128 | + @Entity( name = "RightEntity" ) |
| 129 | + static class RightEntity { |
| 130 | + @Id |
| 131 | + @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "my-sequence-generator" ) |
| 132 | + @SequenceGenerator( name = "my-sequence-generator", sequenceName = "REPLACE_SEQ", allocationSize = 1 ) |
| 133 | + private Long id; |
| 134 | + } |
| 135 | + |
| 136 | + static class MyPhysicalNamingStrategy implements PhysicalNamingStrategy { |
| 137 | + static MyPhysicalNamingStrategy INSTANCE = new MyPhysicalNamingStrategy(); |
| 138 | + |
| 139 | + @Override |
| 140 | + public Identifier toPhysicalCatalogName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
| 141 | + return logicalName; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Identifier toPhysicalSchemaName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
| 146 | + return logicalName; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Identifier toPhysicalTableName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
| 151 | + return logicalName; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Identifier toPhysicalSequenceName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
| 156 | + return Identifier.toIdentifier( |
| 157 | + logicalName.getText().replaceAll( "REPLACE_", "MY_" ), |
| 158 | + logicalName.isQuoted() |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public Identifier toPhysicalColumnName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
| 164 | + return logicalName; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments