File tree Expand file tree Collapse file tree 6 files changed +48
-42
lines changed
typed-ids-hibernate-62/src/main/java/org/framefork/typedIds/bigint/hibernate
typed-ids-hibernate-63/src/main/java/org/framefork/typedIds/bigint/hibernate Expand file tree Collapse file tree 6 files changed +48
-42
lines changed Original file line number Diff line number Diff line change 44import io .hypersistence .utils .hibernate .type .util .ParameterizedParameterType ;
55import org .framefork .typedIds .bigint .ObjectBigIntId ;
66import org .framefork .typedIds .bigint .ObjectBigIntIdTypeUtils ;
7+ import org .hibernate .HibernateException ;
78import org .hibernate .dialect .Dialect ;
89import org .hibernate .engine .spi .SharedSessionContractImplementor ;
910import 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 */
Original file line number Diff line number Diff line change 22
33import com .google .auto .service .AutoService ;
44import org .framefork .typedIds .bigint .ObjectBigIntId ;
5- import org .framefork .typedIds .bigint .hibernate .id .ObjectBigIntIdIdentifierGenerator ;
5+ import org .framefork .typedIds .bigint .hibernate .id .ObjectBigIntIdSequenceStyleGenerator ;
66import org .hibernate .boot .ResourceStreamLocator ;
77import org .hibernate .boot .spi .AdditionalMappingContributions ;
88import 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
Original file line number Diff line number Diff line change 1515import java .util .Objects ;
1616import 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
Original file line number Diff line number Diff line change 44import io .hypersistence .utils .hibernate .type .util .ParameterizedParameterType ;
55import org .framefork .typedIds .bigint .ObjectBigIntId ;
66import org .framefork .typedIds .bigint .ObjectBigIntIdTypeUtils ;
7+ import org .hibernate .HibernateException ;
78import org .hibernate .dialect .Dialect ;
89import org .hibernate .engine .spi .SharedSessionContractImplementor ;
910import 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 */
Original file line number Diff line number Diff line change 22
33import com .google .auto .service .AutoService ;
44import org .framefork .typedIds .bigint .ObjectBigIntId ;
5- import org .framefork .typedIds .bigint .hibernate .id .ObjectBigIntIdIdentifierGenerator ;
5+ import org .framefork .typedIds .bigint .hibernate .id .ObjectBigIntIdSequenceStyleGenerator ;
66import org .hibernate .boot .ResourceStreamLocator ;
77import org .hibernate .boot .spi .AdditionalMappingContributions ;
88import 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
Original file line number Diff line number Diff line change 1515import java .util .Objects ;
1616import 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
You can’t perform that action at this time.
0 commit comments