Legacy generator strategy increment size

This commit is contained in:
Andrea Boriero 2022-03-29 10:04:27 +02:00 committed by Andrea Boriero
parent 7d9eada83e
commit 63ba1787d0
4 changed files with 348 additions and 3 deletions

View File

@ -35,11 +35,14 @@ import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.enhanced.LegacyNamingStrategy;
import org.hibernate.id.enhanced.SingleNamingStrategy;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext;
import org.hibernate.internal.CoreLogging;
@ -434,7 +437,27 @@ public abstract class SimpleValue implements KeyValue {
// default initial value and allocation size per-JPA defaults
params.setProperty( OptimizableGenerator.INITIAL_PARAM, String.valueOf( OptimizableGenerator.DEFAULT_INITIAL_VALUE ) );
params.setProperty( OptimizableGenerator.INCREMENT_PARAM, String.valueOf( OptimizableGenerator.DEFAULT_INCREMENT_SIZE ) );
final ConfigurationService cs = metadata.getMetadataBuildingOptions().getServiceRegistry()
.getService( ConfigurationService.class );
final String idNamingStrategy = cs.getSetting(
AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY,
StandardConverters.STRING,
null
);
if ( LegacyNamingStrategy.STRATEGY_NAME.equals( idNamingStrategy )
|| LegacyNamingStrategy.class.getName().equals( idNamingStrategy )
|| SingleNamingStrategy.STRATEGY_NAME.equals( idNamingStrategy )
|| SingleNamingStrategy.class.getName().equals( idNamingStrategy ) ) {
params.setProperty( OptimizableGenerator.INCREMENT_PARAM, "1" );
}
else {
params.setProperty(
OptimizableGenerator.INCREMENT_PARAM,
String.valueOf( OptimizableGenerator.DEFAULT_INCREMENT_SIZE )
);
}
//init the table here instead of earlier, so that we can get a quoted table name
//TODO: would it be better to simply pass the qualified table name, instead of
// splitting it up into schema/catalog/table names
@ -478,8 +501,6 @@ public abstract class SimpleValue implements KeyValue {
}
// TODO : we should pass along all settings once "config lifecycle" is hashed out...
final ConfigurationService cs = metadata.getMetadataBuildingOptions().getServiceRegistry()
.getService( ConfigurationService.class );
params.put(
IdentifierGenerator.CONTRIBUTOR_NAME,

View File

@ -0,0 +1,303 @@
package org.hibernate.orm.test.schemaupdate.idgenerator;
import java.io.File;
import java.nio.file.Files;
import java.util.EnumSet;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.id.enhanced.LegacyNamingStrategy;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.orm.junit.BaseUnitTest;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;
import static org.junit.Assert.assertTrue;
@BaseUnitTest
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsSequences.class)
public class SequenceGeneratorIncrementTest {
private File output;
private ServiceRegistry ssr;
private MetadataImplementor metadata;
@BeforeEach
public void setUp() throws Exception {
output = File.createTempFile( "update_script", ".sql" );
output.deleteOnExit();
}
@AfterEach
public void tearDown() {
new SchemaExport().drop( EnumSet.of( TargetType.DATABASE ), metadata );
ServiceRegistryBuilder.destroy( ssr );
}
@Test
public void testSequence() throws Exception {
buildMetadata( TestEntity.class );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 50" ) );
}
@Test
public void testSequenceLegacy() throws Exception {
buildMetadata( TestEntity.class, LegacyNamingStrategy.class.getName() );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 1" ) );
}
@Test
public void testSequenceNoMatchingGenerator() throws Exception {
buildMetadata( TestEntity2.class );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 50" ) );
}
@Test
public void testSequenceNoMatchingGeneratorLegacy() throws Exception {
buildMetadata( TestEntity2.class, LegacyNamingStrategy.class.getName() );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 1" ) );
}
@Test
public void testSequenceGeneratorWithDefaultAllocationSize() throws Exception {
buildMetadata( TestEntity3.class );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 50" ) );
}
@Test
public void testSequenceGeneratorWithDefaultAllocationSizeLegacy() throws Exception {
buildMetadata( TestEntity3.class, LegacyNamingStrategy.class.getName() );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 50" ) );
}
@Test
public void testSequenceGeneratorWithAllocationSize() throws Exception {
buildMetadata( TestEntity4.class );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 20" ) );
}
@Test
public void testSequenceGeneratorWithAllocationSizeLegacy() throws Exception {
buildMetadata( TestEntity4.class, LegacyNamingStrategy.class.getName() );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 20" ) );
}
@Test
public void testSequenceGenericGeneratorWithDefaultAllocationSize() throws Exception {
buildMetadata( TestEntity5.class );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 50" ) );
}
@Test
public void testSequenceGenericGeneratorWithDefaultAllocationSizeLegacy() throws Exception {
buildMetadata( TestEntity5.class, LegacyNamingStrategy.class.getName() );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 1" ) );
}
@Test
public void testSequenceGenericGeneratorWithAllocationSize() throws Exception {
buildMetadata( TestEntity6.class );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 10" ) );
}
@Test
public void testSequenceGenericGeneratorWithAllocationSizeLegacy() throws Exception {
buildMetadata( TestEntity6.class , LegacyNamingStrategy.class.getName());
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 10" ) );
}
@Test
public void testSequenceHbm() throws Exception {
buildMetadata( "org/hibernate/orm/test/schemaupdate/idgenerator/sequence.hbm.xml" );
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 50" ) );
}
@Test
public void testSequenceHbmLegacy() throws Exception {
buildMetadata(
"org/hibernate/orm/test/schemaupdate/idgenerator/sequence.hbm.xml",
LegacyNamingStrategy.class.getName()
);
createSchema();
final String fileContent = new String( Files.readAllBytes( output.toPath() ) );
assertTrue( fileContent.contains( "increment by 1" ) );
}
private void buildMetadata(Class annotatedClass) {
buildMetadata( annotatedClass, null, null );
}
private void buildMetadata(String hbm) {
buildMetadata( null, hbm, null );
}
private void buildMetadata(Class annotatedClass, String namingStrategy) {
buildMetadata( annotatedClass, null, namingStrategy );
}
private void buildMetadata(String hbm, String namingStrategy) {
buildMetadata( null, hbm, namingStrategy );
}
private void buildMetadata(Class annotatedClass, String hbm, String namingStrategy) {
StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySetting( AvailableSettings.FORMAT_SQL, "false" );
if ( namingStrategy != null ) {
standardServiceRegistryBuilder.applySetting(
AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY,
namingStrategy
);
}
ssr = standardServiceRegistryBuilder.build();
final MetadataSources metadataSources = new MetadataSources( ssr );
if ( annotatedClass != null ) {
metadataSources.addAnnotatedClass( annotatedClass );
}
if ( hbm != null ) {
metadataSources.addResource( hbm );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.validate();
}
private void createSchema() {
final SchemaExport schemaExport = new SchemaExport();
schemaExport.setOutputFile( output.getAbsolutePath() );
schemaExport.create( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );
}
@Entity(name = "TestEntity")
public static class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
}
@Entity(name = "TestEntity2")
public static class TestEntity2 {
@Id
@GeneratedValue(generator = "no_seq_gen")
private Long id;
private String name;
}
@Entity(name = "TestEntity3")
public static class TestEntity3 {
@Id
@GeneratedValue(generator = "seq_gen")
@SequenceGenerator(name = "seq_gen")
private Long id;
private String name;
}
@Entity(name = "TestEntity4")
public static class TestEntity4 {
@Id
@GeneratedValue(generator = "seq_gen")
@SequenceGenerator(name = "seq_gen", allocationSize = 20)
private Long id;
private String name;
}
@Entity(name = "TestEntity5")
public static class TestEntity5 {
@Id
@GeneratedValue(generator = "ID_GENERATOR")
@GenericGenerator(name = "ID_GENERATOR", strategy = "enhanced-sequence")
private Long id;
private String name;
}
@Entity(name = "TestEntity6")
public static class TestEntity6 {
@Id
@GeneratedValue(generator = "ID_GENERATOR")
@GenericGenerator(name = "ID_GENERATOR", strategy = "enhanced-sequence", parameters = @org.hibernate.annotations.Parameter(
name = "increment_size",
value = "10"
)
)
private Long id;
private String name;
}
}

View File

@ -0,0 +1,8 @@
package org.hibernate.orm.test.schemaupdate.idgenerator;
public class TestEntity {
private Long id;
private String name;
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.orm.test.schemaupdate.idgenerator.TestEntity">
<id name="id" type="long">
<column name="id" />
<generator class="sequence"/>
</id>
<property name="name" />
</class>
</hibernate-mapping>