Skip to content

Commit 2b56cd9

Browse files
committed
refactor ObjectBigIntIdIdentifierGenerator
1 parent 2fd1b60 commit 2b56cd9

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

modules/typed-ids-hibernate-62/src/main/java/org/framefork/typedIds/bigint/hibernate/ObjectBigIntIdType.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.hypersistence.utils.hibernate.type.util.ParameterizedParameterType;
55
import org.framefork.typedIds.bigint.ObjectBigIntId;
66
import org.framefork.typedIds.bigint.ObjectBigIntIdTypeUtils;
7+
import org.hibernate.HibernateException;
78
import org.hibernate.dialect.Dialect;
89
import org.hibernate.engine.spi.SharedSessionContractImplementor;
910
import org.hibernate.metamodel.model.domain.DomainType;
@@ -145,6 +146,21 @@ public MutabilityPlan<ObjectBigIntId<?>> getExposedMutabilityPlan()
145146
return javaTypeDescriptor.getMutabilityPlan();
146147
}
147148

149+
public ObjectBigIntId<?> wrapJdbcValue(final Object value)
150+
{
151+
if (value instanceof Long longValue) {
152+
return javaTypeDescriptor.wrap(longValue, null);
153+
}
154+
if (value instanceof Number numberValue) {
155+
return wrapJdbcValue(numberValue.longValue());
156+
}
157+
if (getReturnedClass().isInstance(value)) {
158+
return getReturnedClass().cast(value);
159+
}
160+
161+
throw new HibernateException("Could not convert '%s' to '%s'".formatted(value.getClass().getName(), getReturnedClass()));
162+
}
163+
148164
/**
149165
* the nullSafeGet() is delegated here in the {@link ImmutableType}
150166
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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.ObjectBigIntIdIdentifierGenerator;
5+
import org.framefork.typedIds.bigint.hibernate.id.ObjectBigIntIdSequenceStyleGenerator;
66
import org.hibernate.boot.ResourceStreamLocator;
77
import org.hibernate.boot.spi.AdditionalMappingContributions;
88
import org.hibernate.boot.spi.AdditionalMappingContributor;
@@ -47,7 +47,7 @@ public void contribute(
4747
private void remapIdentifierGeneratorStrategy(final SimpleValue identifier)
4848
{
4949
var newIdentifierGeneratorStrategy = switch (identifier.getIdentifierGeneratorStrategy()) {
50-
case "org.hibernate.id.enhanced.SequenceStyleGenerator" -> ObjectBigIntIdIdentifierGenerator.class.getName();
50+
case "org.hibernate.id.enhanced.SequenceStyleGenerator" -> ObjectBigIntIdSequenceStyleGenerator.class.getName();
5151
default -> identifier.getIdentifierGeneratorStrategy();
5252
};
5353

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,21 @@
1515
import java.util.Objects;
1616
import java.util.Properties;
1717

18-
public class ObjectBigIntIdIdentifierGenerator extends SequenceStyleGenerator
18+
/**
19+
* This class handles making Hibernate think it's generating a primitive long type instead of a custom type,
20+
* and once Hibernate internals do their job, it then handles wrapping the generated long value in an ObjectBigIntId.
21+
*/
22+
public class ObjectBigIntIdSequenceStyleGenerator extends SequenceStyleGenerator
1923
{
2024

2125
@Nullable
2226
private ObjectBigIntIdType objectBigIntIdType;
2327

2428
@Override
2529
public Object generate(final SharedSessionContractImplementor session, final Object object)
26-
{
27-
return wrapToCustomType(toLong(super.generate(session, object)));
28-
}
29-
30-
private Object wrapToCustomType(final Long generatedId)
3130
{
3231
var idType = Objects.requireNonNull(objectBigIntIdType, "objectBigIntIdType must not be null");
33-
return idType.getExpressibleJavaType().wrap(generatedId, null);
34-
}
35-
36-
private Long toLong(final Object value)
37-
{
38-
if (value instanceof Long longValue) {
39-
return longValue;
40-
}
41-
if (value instanceof Number numberValue) {
42-
return numberValue.longValue();
43-
}
44-
45-
throw new HibernateException("Could not convert '%s' to 'Long'".formatted(value.getClass().getName()));
32+
return idType.wrapJdbcValue(super.generate(session, object));
4633
}
4734

4835
@Override

modules/typed-ids-hibernate-63/src/main/java/org/framefork/typedIds/bigint/hibernate/ObjectBigIntIdType.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.hypersistence.utils.hibernate.type.util.ParameterizedParameterType;
55
import org.framefork.typedIds.bigint.ObjectBigIntId;
66
import org.framefork.typedIds.bigint.ObjectBigIntIdTypeUtils;
7+
import org.hibernate.HibernateException;
78
import org.hibernate.dialect.Dialect;
89
import org.hibernate.engine.spi.SharedSessionContractImplementor;
910
import org.hibernate.metamodel.model.domain.DomainType;
@@ -145,6 +146,21 @@ public MutabilityPlan<ObjectBigIntId<?>> getExposedMutabilityPlan()
145146
return javaTypeDescriptor.getMutabilityPlan();
146147
}
147148

149+
public ObjectBigIntId<?> wrapJdbcValue(final Object value)
150+
{
151+
if (value instanceof Long longValue) {
152+
return javaTypeDescriptor.wrap(longValue, null);
153+
}
154+
if (value instanceof Number numberValue) {
155+
return wrapJdbcValue(numberValue.longValue());
156+
}
157+
if (getReturnedClass().isInstance(value)) {
158+
return getReturnedClass().cast(value);
159+
}
160+
161+
throw new HibernateException("Could not convert '%s' to '%s'".formatted(value.getClass().getName(), getReturnedClass()));
162+
}
163+
148164
/**
149165
* the nullSafeGet() is delegated here in the {@link ImmutableType}
150166
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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.ObjectBigIntIdIdentifierGenerator;
5+
import org.framefork.typedIds.bigint.hibernate.id.ObjectBigIntIdSequenceStyleGenerator;
66
import org.hibernate.boot.ResourceStreamLocator;
77
import org.hibernate.boot.spi.AdditionalMappingContributions;
88
import org.hibernate.boot.spi.AdditionalMappingContributor;
@@ -47,7 +47,7 @@ public void contribute(
4747
private void remapIdentifierGeneratorStrategy(final SimpleValue identifier)
4848
{
4949
var newIdentifierGeneratorStrategy = switch (identifier.getIdentifierGeneratorStrategy()) {
50-
case "org.hibernate.id.enhanced.SequenceStyleGenerator" -> ObjectBigIntIdIdentifierGenerator.class.getName();
50+
case "org.hibernate.id.enhanced.SequenceStyleGenerator" -> ObjectBigIntIdSequenceStyleGenerator.class.getName();
5151
default -> identifier.getIdentifierGeneratorStrategy();
5252
};
5353

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,21 @@
1515
import java.util.Objects;
1616
import java.util.Properties;
1717

18-
public class ObjectBigIntIdIdentifierGenerator extends SequenceStyleGenerator
18+
/**
19+
* This class handles making Hibernate think it's generating a primitive long type instead of a custom type,
20+
* and once Hibernate internals do their job, it then handles wrapping the generated long value in an ObjectBigIntId.
21+
*/
22+
public class ObjectBigIntIdSequenceStyleGenerator extends SequenceStyleGenerator
1923
{
2024

2125
@Nullable
2226
private ObjectBigIntIdType objectBigIntIdType;
2327

2428
@Override
2529
public Object generate(final SharedSessionContractImplementor session, final Object object)
26-
{
27-
return wrapToCustomType(toLong(super.generate(session, object)));
28-
}
29-
30-
private Object wrapToCustomType(final Long generatedId)
3130
{
3231
var idType = Objects.requireNonNull(objectBigIntIdType, "objectBigIntIdType must not be null");
33-
return idType.getExpressibleJavaType().wrap(generatedId, null);
34-
}
35-
36-
private Long toLong(final Object value)
37-
{
38-
if (value instanceof Long longValue) {
39-
return longValue;
40-
}
41-
if (value instanceof Number numberValue) {
42-
return numberValue.longValue();
43-
}
44-
45-
throw new HibernateException("Could not convert '%s' to 'Long'".formatted(value.getClass().getName()));
32+
return idType.wrapJdbcValue(super.generate(session, object));
4633
}
4734

4835
@Override

0 commit comments

Comments
 (0)