Skip to content

Commit 28f715f

Browse files
committed
add support for Hibernate ORM 7.0
1 parent d77f681 commit 28f715f

File tree

15 files changed

+298
-241
lines changed

15 files changed

+298
-241
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ For seamless type support in Hibernate ORM, you should pick one of the following
1414

1515
| Hibernate Version | Artifact |
1616
|-------------------------------|----------------------------------------------------------------------------------------------------------------------|
17+
| 7.0 | [org.framefork:typed-ids-hibernate-70](https://central.sonatype.com/artifact/org.framefork/typed-ids-hibernate-70) |
1718
| 6.6, 6.5, 6.4, and 6.3 | [org.framefork:typed-ids-hibernate-63](https://central.sonatype.com/artifact/org.framefork/typed-ids-hibernate-63) |
1819
| 6.2 | [org.framefork:typed-ids-hibernate-62](https://central.sonatype.com/artifact/org.framefork/typed-ids-hibernate-62) |
1920
| 6.1 | [org.framefork:typed-ids-hibernate-61](https://central.sonatype.com/artifact/org.framefork/typed-ids-hibernate-61) |

gradle/libs.versions.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ postgresql = { module = "org.postgresql:postgresql", version = "42.7.4" }
2828
mysql = { module = "com.mysql:mysql-connector-j", version = "8.3.0" }
2929
logback-classic = { module = "ch.qos.logback:logback-classic", version = "1.5.12" }
3030
hibernate-orm-v61 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.1.7.Final" }
31-
hibernate-orm-v62 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.2.36.Final" }
31+
hibernate-orm-v62 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.2.41.Final" }
3232
hibernate-orm-v63 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.3.2.Final" }
3333
hibernate-orm-v64 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.4.10.Final" }
3434
hibernate-orm-v65 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.5.3.Final" }
35-
hibernate-orm-v66 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.6.12.Final" }
35+
hibernate-orm-v66 = { group = "org.hibernate.orm", name = "hibernate-core", version = "6.6.22.Final" }
36+
hibernate-orm-v70 = { group = "org.hibernate.orm", name = "hibernate-core", version = "7.0.7.Final" }
37+
hibernate-models-v70 = { group = "org.hibernate.models", name = "hibernate-models", version = "1.0.0" }
3638
hypersistence-utils-hibernate61 = { group = "io.hypersistence", name = "hypersistence-utils-hibernate-60", version = "3.9.4" }
3739
hypersistence-utils-hibernate62 = { group = "io.hypersistence", name = "hypersistence-utils-hibernate-62", version = "3.9.4" }
38-
hypersistence-utils-hibernate63 = { group = "io.hypersistence", name = "hypersistence-utils-hibernate-63", version = "3.9.9" }
40+
hypersistence-utils-hibernate63 = { group = "io.hypersistence", name = "hypersistence-utils-hibernate-63", version = "3.10.3" }
41+
hypersistence-utils-hibernate70 = { group = "io.hypersistence", name = "hypersistence-utils-hibernate-70", version = "3.10.3" }
3942
hypersistence-tsid = { module = "io.hypersistence:hypersistence-tsid", version = "2.1.4" }
4043
kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.7.0" }

modules/typed-ids-hibernate-70-testing/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ plugins {
55
dependencies {
66
api(project(":typed-ids-testing"))
77

8-
api(libs.hibernate.orm.v63)
9-
api(libs.hypersistence.utils.hibernate63)
8+
api(libs.hibernate.orm.v70)
9+
api(libs.hypersistence.utils.hibernate70)
1010
}

modules/typed-ids-hibernate-70/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ plugins {
44

55
dependencies {
66
api(project(":typed-ids"))
7-
api(libs.hibernate.orm.v63)
7+
api(libs.hibernate.orm.v70)
8+
compileOnly(libs.hibernate.models.v70) // this is really a runtime dependency, but in runtime the version from hibernate is provided
89

910
compileOnly(libs.jetbrains.annotations)
1011

modules/typed-ids-hibernate-70/src/main/java/org/framefork/typedIds/bigint/hibernate/ObjectBigIntIdJavaType.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.framefork.typedIds.bigint.ObjectBigIntId;
44
import org.framefork.typedIds.bigint.ObjectBigIntIdTypeUtils;
55
import org.framefork.typedIds.common.ReflectionHacks;
6+
import org.framefork.typedIds.common.hibernate.ParameterizedTypeUtils;
67
import org.hibernate.type.SqlTypes;
78
import org.hibernate.type.descriptor.WrapperOptions;
89
import org.hibernate.type.descriptor.java.BasicJavaType;
@@ -25,6 +26,7 @@
2526
import java.util.Objects;
2627
import java.util.Properties;
2728

29+
@SuppressWarnings("removal") // DynamicParameterizedType usage
2830
public class ObjectBigIntIdJavaType implements BasicJavaType<ObjectBigIntId<?>>, DynamicParameterizedType, Serializable
2931
{
3032

@@ -40,20 +42,10 @@ public ObjectBigIntIdJavaType()
4042
this.inner = LongJavaType.INSTANCE;
4143
}
4244

43-
@SuppressWarnings("unchecked")
4445
@Override
4546
public void setParameterValues(final Properties parameters)
4647
{
47-
var parameterType = (ParameterType) parameters.get(PARAMETER_TYPE);
48-
if (parameterType != null) {
49-
this.identifierClass = (Class<ObjectBigIntId<?>>) parameterType.getReturnedClass();
50-
51-
} else {
52-
String entityClass = Objects.requireNonNull(parameters.get(ENTITY), "parameters.get(ENTITY) must not be null").toString();
53-
String propertyName = Objects.requireNonNull(parameters.get(PROPERTY), "parameters.get(PROPERTY) must not be null").toString();
54-
55-
this.identifierClass = ReflectionHacks.getFieldTypeChecked(entityClass, propertyName, ObjectBigIntId.class);
56-
}
48+
this.identifierClass = ParameterizedTypeUtils.getReturnedClass(parameters, ObjectBigIntId.class);
5749

5850
if (!ObjectBigIntId.class.isAssignableFrom(identifierClass)) {
5951
throw new IllegalArgumentException("Type %s is not a subtype of %s".formatted(identifierClass, ObjectBigIntId.class));

modules/typed-ids-hibernate-70/src/main/java/org/framefork/typedIds/bigint/hibernate/ObjectBigIntIdTypeGenerationMetadataContributor.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import com.google.auto.service.AutoService;
44
import org.framefork.typedIds.bigint.ObjectBigIntId;
5-
import org.framefork.typedIds.bigint.hibernate.id.ObjectBigIntIdIdentityGenerator;
6-
import org.framefork.typedIds.bigint.hibernate.id.ObjectBigIntIdSequenceStyleGenerator;
5+
import org.framefork.typedIds.bigint.hibernate.id.ObjectBigIntIdGeneratorCreator;
76
import org.hibernate.boot.ResourceStreamLocator;
87
import org.hibernate.boot.spi.AdditionalMappingContributions;
98
import org.hibernate.boot.spi.AdditionalMappingContributor;
@@ -12,6 +11,7 @@
1211
import org.hibernate.mapping.PersistentClass;
1312
import org.hibernate.mapping.SimpleValue;
1413

14+
@SuppressWarnings("unused")
1515
@AutoService(AdditionalMappingContributor.class)
1616
public class ObjectBigIntIdTypeGenerationMetadataContributor implements AdditionalMappingContributor
1717
{
@@ -40,21 +40,17 @@ public void contribute(
4040
continue;
4141
}
4242

43-
remapIdentifierGeneratorStrategy(simpleValueIdentifier);
43+
remapIdentifierCustomIdGeneratorCreator(simpleValueIdentifier);
4444
}
4545
}
4646
}
4747

48-
private void remapIdentifierGeneratorStrategy(final SimpleValue identifier)
48+
private void remapIdentifierCustomIdGeneratorCreator(final SimpleValue identifier)
4949
{
50-
var newIdentifierGeneratorStrategy = switch (identifier.getIdentifierGeneratorStrategy()) {
51-
case "org.hibernate.id.enhanced.SequenceStyleGenerator" -> ObjectBigIntIdSequenceStyleGenerator.class.getName();
52-
case "org.hibernate.id.IdentityGenerator" -> ObjectBigIntIdIdentityGenerator.class.getName();
53-
case "identity" -> ObjectBigIntIdIdentityGenerator.class.getName();
54-
default -> identifier.getIdentifierGeneratorStrategy();
55-
};
56-
57-
identifier.setIdentifierGeneratorStrategy(newIdentifierGeneratorStrategy);
50+
var currentCreator = identifier.getCustomIdGeneratorCreator();
51+
if (currentCreator != null && !currentCreator.isAssigned()) {
52+
identifier.setCustomIdGeneratorCreator(new ObjectBigIntIdGeneratorCreator(currentCreator));
53+
}
5854
}
5955

6056
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.framefork.typedIds.bigint.hibernate.id;
2+
3+
import org.hibernate.annotations.IdGeneratorType;
4+
import org.hibernate.boot.model.internal.GeneratorAnnotationHelper;
5+
import org.hibernate.generator.Generator;
6+
import org.hibernate.generator.GeneratorCreationContext;
7+
import org.hibernate.id.enhanced.SequenceStyleGenerator;
8+
import org.hibernate.mapping.GeneratorCreator;
9+
10+
import java.util.Objects;
11+
12+
public class ObjectBigIntIdGeneratorCreator implements GeneratorCreator
13+
{
14+
15+
private final GeneratorCreator originalCreator;
16+
17+
public ObjectBigIntIdGeneratorCreator(final GeneratorCreator originalCreator)
18+
{
19+
this.originalCreator = Objects.requireNonNull(originalCreator, "originalCreator must not be null");
20+
}
21+
22+
@Override
23+
public Generator createGenerator(GeneratorCreationContext context)
24+
{
25+
var originalGenerator = originalCreator.createGenerator(context);
26+
27+
if (originalGenerator instanceof SequenceStyleGenerator) {
28+
return prepareForUse(new ObjectBigIntIdSequenceStyleGenerator(), context);
29+
30+
} else {
31+
// For any other generator type, return the original
32+
// Note: the IdentityGenerator seems to be capable of handling custom types now
33+
return originalGenerator;
34+
}
35+
}
36+
37+
private static <T extends Generator> T prepareForUse(final T generator, final GeneratorCreationContext context)
38+
{
39+
GeneratorAnnotationHelper.<IdGeneratorType>prepareForUse(generator, null, null, null, null, context);
40+
return generator;
41+
}
42+
43+
@Override
44+
public boolean isAssigned()
45+
{
46+
return originalCreator.isAssigned();
47+
}
48+
49+
}

modules/typed-ids-hibernate-70/src/main/java/org/framefork/typedIds/bigint/hibernate/id/ObjectBigIntIdIdentityGenerator.java

Lines changed: 0 additions & 121 deletions
This file was deleted.

modules/typed-ids-hibernate-70/src/main/java/org/framefork/typedIds/bigint/hibernate/id/ObjectBigIntIdSequenceStyleGenerator.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.framefork.typedIds.bigint.hibernate.id;
22

33
import org.framefork.typedIds.bigint.hibernate.ObjectBigIntIdType;
4+
import org.framefork.typedIds.common.hibernate.TypeOverrideGeneratorCreationContext;
45
import org.hibernate.HibernateException;
6+
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
57
import org.hibernate.engine.spi.SharedSessionContractImplementor;
8+
import org.hibernate.generator.GeneratorCreationContext;
69
import org.hibernate.id.enhanced.SequenceStyleGenerator;
7-
import org.hibernate.service.ServiceRegistry;
810
import org.hibernate.type.CustomType;
911
import org.hibernate.type.Type;
1012
import org.hibernate.type.descriptor.java.spi.JavaTypeBasicAdaptor;
@@ -33,17 +35,24 @@ public Object generate(final SharedSessionContractImplementor session, final Obj
3335
}
3436

3537
@Override
36-
public void configure(final Type type, final Properties parameters, final ServiceRegistry serviceRegistry)
38+
public void configure(GeneratorCreationContext creationContext, Properties parameters)
3739
{
38-
this.objectBigIntIdType = toObjectBigIntIdType(type);
40+
this.objectBigIntIdType = toObjectBigIntIdType(creationContext.getType());
3941

4042
var primitiveType = new ImmutableNamedBasicTypeImpl<>(
41-
new JavaTypeBasicAdaptor<Long>(Long.class),
43+
new JavaTypeBasicAdaptor<>(Long.class),
4244
toJdbcType(objectBigIntIdType),
4345
"bigint"
4446
);
4547

46-
super.configure(primitiveType, parameters, serviceRegistry);
48+
super.configure(new TypeOverrideGeneratorCreationContext(creationContext, primitiveType), parameters);
49+
}
50+
51+
@Override
52+
public void initialize(SqlStringGenerationContext context)
53+
{
54+
// Initialize the parent to set up the database structure
55+
super.initialize(context);
4756
}
4857

4958
private ObjectBigIntIdType toObjectBigIntIdType(final Type type)

0 commit comments

Comments
 (0)