-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31947 from yrodiere/optimizers
Allow configuring the default ID optimizer and default to pooled-lo
- Loading branch information
Showing
24 changed files
with
406 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...st/java/io/quarkus/hibernate/orm/mapping/id/optimizer/AbstractIdOptimizerDefaultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.assertj.core.api.AbstractObjectAssert; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.id.OptimizableGenerator; | ||
import org.hibernate.id.enhanced.Optimizer; | ||
import org.hibernate.id.enhanced.PooledLoOptimizer; | ||
import org.hibernate.id.enhanced.PooledOptimizer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.narayana.jta.QuarkusTransaction; | ||
|
||
public abstract class AbstractIdOptimizerDefaultTest { | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Inject | ||
Session session; | ||
|
||
abstract Class<?> defaultOptimizerType(); | ||
|
||
@Test | ||
public void defaults() { | ||
assertThat(List.of( | ||
EntityWithDefaultGenerator.class, | ||
EntityWithGenericGenerator.class, | ||
EntityWithSequenceGenerator.class, | ||
EntityWithTableGenerator.class)) | ||
.allSatisfy(c -> assertOptimizer(c).isInstanceOf(defaultOptimizerType())); | ||
} | ||
|
||
@Test | ||
public void explicitOverrides() { | ||
assertOptimizer(EntityWithGenericGeneratorAndPooledOptimizer.class) | ||
.isInstanceOf(PooledOptimizer.class); | ||
assertOptimizer(EntityWithGenericGeneratorAndPooledLoOptimizer.class) | ||
.isInstanceOf(PooledLoOptimizer.class); | ||
} | ||
|
||
@Test | ||
public void ids() { | ||
for (long i = 1; i <= 51; i++) { | ||
assertThat(QuarkusTransaction.requiringNew().call(() -> { | ||
var entity = new EntityWithSequenceGenerator(); | ||
session.persist(entity); | ||
return entity.id; | ||
})) | ||
.isEqualTo(i); | ||
} | ||
} | ||
|
||
AbstractObjectAssert<?, Optimizer> assertOptimizer(Class<?> entityType) { | ||
return assertThat(SchemaUtil.getGenerator(sessionFactory, entityType)) | ||
.as("ID generator for entity type " + entityType.getSimpleName()) | ||
.asInstanceOf(InstanceOfAssertFactories.type(OptimizableGenerator.class)) | ||
.extracting(OptimizableGenerator::getOptimizer) | ||
.as("ID optimizer for entity type " + entityType.getSimpleName()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...c/test/java/io/quarkus/hibernate/orm/mapping/id/optimizer/EntityWithDefaultGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
@Entity | ||
public class EntityWithDefaultGenerator { | ||
|
||
@Id | ||
@GeneratedValue | ||
Long id; | ||
|
||
public EntityWithDefaultGenerator() { | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...c/test/java/io/quarkus/hibernate/orm/mapping/id/optimizer/EntityWithGenericGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
import org.hibernate.annotations.GenericGenerator; | ||
import org.hibernate.id.enhanced.SequenceStyleGenerator; | ||
|
||
@Entity | ||
public class EntityWithGenericGenerator { | ||
|
||
@Id | ||
@GeneratedValue(generator = "gen_gen") | ||
@GenericGenerator(name = "gen_gen", type = SequenceStyleGenerator.class) | ||
Long id; | ||
|
||
public EntityWithGenericGenerator() { | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...us/hibernate/orm/mapping/id/optimizer/EntityWithGenericGeneratorAndPooledLoOptimizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
import org.hibernate.annotations.GenericGenerator; | ||
import org.hibernate.annotations.Parameter; | ||
import org.hibernate.id.OptimizableGenerator; | ||
import org.hibernate.id.enhanced.SequenceStyleGenerator; | ||
|
||
@Entity | ||
public class EntityWithGenericGeneratorAndPooledLoOptimizer { | ||
|
||
@Id | ||
@GeneratedValue(generator = "gen_gen_pooled_lo") | ||
@GenericGenerator(name = "gen_gen_pooled_lo", type = SequenceStyleGenerator.class, parameters = @Parameter(name = OptimizableGenerator.OPT_PARAM, value = "pooled-lo")) | ||
Long id; | ||
|
||
public EntityWithGenericGeneratorAndPooledLoOptimizer() { | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...rkus/hibernate/orm/mapping/id/optimizer/EntityWithGenericGeneratorAndPooledOptimizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
import org.hibernate.annotations.GenericGenerator; | ||
import org.hibernate.annotations.Parameter; | ||
import org.hibernate.id.OptimizableGenerator; | ||
import org.hibernate.id.enhanced.SequenceStyleGenerator; | ||
|
||
@Entity | ||
public class EntityWithGenericGeneratorAndPooledOptimizer { | ||
|
||
@Id | ||
@GeneratedValue(generator = "gen_gen_pooled_lo") | ||
@GenericGenerator(name = "gen_gen_pooled_lo", type = SequenceStyleGenerator.class, parameters = @Parameter(name = OptimizableGenerator.OPT_PARAM, value = "pooled")) | ||
Long id; | ||
|
||
public EntityWithGenericGeneratorAndPooledOptimizer() { | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
.../test/java/io/quarkus/hibernate/orm/mapping/id/optimizer/EntityWithSequenceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.SequenceGenerator; | ||
|
||
@Entity | ||
public class EntityWithSequenceGenerator { | ||
|
||
@Id | ||
@GeneratedValue(generator = "seq_gen") | ||
@SequenceGenerator(name = "seq_gen") | ||
Long id; | ||
|
||
public EntityWithSequenceGenerator() { | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...src/test/java/io/quarkus/hibernate/orm/mapping/id/optimizer/EntityWithTableGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.TableGenerator; | ||
|
||
@Entity | ||
public class EntityWithTableGenerator { | ||
|
||
@Id | ||
@GeneratedValue(generator = "tab_gen") | ||
@TableGenerator(name = "tab_gen") | ||
Long id; | ||
|
||
public EntityWithTableGenerator() { | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...est/java/io/quarkus/hibernate/orm/mapping/id/optimizer/IdOptimizerDefaultDefaultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import org.hibernate.id.enhanced.PooledLoOptimizer; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class IdOptimizerDefaultDefaultTest extends AbstractIdOptimizerDefaultTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(EntityWithDefaultGenerator.class, EntityWithGenericGenerator.class, | ||
EntityWithSequenceGenerator.class, EntityWithTableGenerator.class, | ||
EntityWithGenericGeneratorAndPooledOptimizer.class, | ||
EntityWithGenericGeneratorAndPooledLoOptimizer.class) | ||
.addClasses(SchemaUtil.class)) | ||
.withConfigurationResource("application.properties"); | ||
|
||
@Override | ||
Class<?> defaultOptimizerType() { | ||
return PooledLoOptimizer.class; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c/test/java/io/quarkus/hibernate/orm/mapping/id/optimizer/IdOptimizerDefaultNoneTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.hibernate.orm.mapping.id.optimizer; | ||
|
||
import org.hibernate.id.enhanced.NoopOptimizer; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class IdOptimizerDefaultNoneTest extends AbstractIdOptimizerDefaultTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(EntityWithDefaultGenerator.class, EntityWithGenericGenerator.class, | ||
EntityWithSequenceGenerator.class, EntityWithTableGenerator.class, | ||
EntityWithGenericGeneratorAndPooledOptimizer.class, | ||
EntityWithGenericGeneratorAndPooledLoOptimizer.class) | ||
.addClasses(SchemaUtil.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-orm.mapping.id.optimizer.default", "none"); | ||
|
||
@Override | ||
@Disabled("The 'none' optimizer will produce a different stream of IDs (1 then 51 then 101 then ...)") | ||
public void ids() { | ||
super.ids(); | ||
} | ||
|
||
@Override | ||
Class<?> defaultOptimizerType() { | ||
return NoopOptimizer.class; | ||
} | ||
} |
Oops, something went wrong.