|
| 1 | +package org.framefork.typedIds.bigint.hibernate.id; |
| 2 | + |
| 3 | +import org.framefork.typedIds.bigint.hibernate.ObjectBigIntIdType; |
| 4 | +import org.framefork.typedIds.common.ReflectionHacks; |
| 5 | +import org.hibernate.HibernateException; |
| 6 | +import org.hibernate.boot.model.relational.SqlStringGenerationContext; |
| 7 | +import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; |
| 8 | +import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; |
| 9 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 10 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 11 | +import org.hibernate.id.IdentityGenerator; |
| 12 | +import org.hibernate.id.PostInsertIdentityPersister; |
| 13 | +import org.hibernate.id.insert.Binder; |
| 14 | +import org.hibernate.id.insert.IdentifierGeneratingInsert; |
| 15 | +import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate; |
| 16 | +import org.hibernate.jdbc.Expectation; |
| 17 | +import org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping; |
| 18 | +import org.hibernate.service.ServiceRegistry; |
| 19 | +import org.hibernate.sql.model.ast.builder.TableInsertBuilder; |
| 20 | +import org.hibernate.type.CustomType; |
| 21 | +import org.hibernate.type.Type; |
| 22 | +import org.hibernate.type.descriptor.java.spi.JavaTypeBasicAdaptor; |
| 23 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 24 | +import org.hibernate.type.internal.ImmutableNamedBasicTypeImpl; |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | + |
| 27 | +import java.lang.reflect.InvocationHandler; |
| 28 | +import java.lang.reflect.Method; |
| 29 | +import java.lang.reflect.Proxy; |
| 30 | +import java.sql.PreparedStatement; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.Properties; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class handles making Hibernate think it's generating a primitive long type instead of a custom type, |
| 36 | + * and once Hibernate internals do their job, it then handles wrapping the generated long value in an ObjectBigIntId. |
| 37 | + */ |
| 38 | +public class ObjectBigIntIdIdentityGenerator extends IdentityGenerator |
| 39 | +{ |
| 40 | + |
| 41 | + @Nullable |
| 42 | + private ObjectBigIntIdType objectBigIntIdType; |
| 43 | + @Nullable |
| 44 | + private ImmutableNamedBasicTypeImpl<Long> primitiveType; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void configure(final Type type, final Properties parameters, final ServiceRegistry serviceRegistry) |
| 48 | + { |
| 49 | + this.objectBigIntIdType = toObjectBigIntIdType(type); |
| 50 | + this.primitiveType = new ImmutableNamedBasicTypeImpl<>( |
| 51 | + new JavaTypeBasicAdaptor<Long>(Long.class), |
| 52 | + toJdbcType(objectBigIntIdType), |
| 53 | + "bigint" |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + private ObjectBigIntIdType toObjectBigIntIdType(final Type type) |
| 58 | + { |
| 59 | + if (type instanceof CustomType<?> customType) { |
| 60 | + var userType = customType.getUserType(); |
| 61 | + if (userType instanceof ObjectBigIntIdType objectBigIntIdType) { |
| 62 | + return objectBigIntIdType; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + throw new HibernateException("The given type is expected to be a CustomType wrapper over a %s, but was '%s' instead".formatted(ObjectBigIntIdType.class.getSimpleName(), type)); |
| 67 | + } |
| 68 | + |
| 69 | + private JdbcType toJdbcType(final ObjectBigIntIdType objectBigIntIdType) |
| 70 | + { |
| 71 | + return objectBigIntIdType.getJdbcType(null); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(final PostInsertIdentityPersister persister) |
| 76 | + { |
| 77 | + var persisterProxy = proxyPersister(persister); |
| 78 | + |
| 79 | + var originalDelegate = super.getGeneratedIdentifierDelegate(persisterProxy); |
| 80 | + |
| 81 | + return new InsertGeneratedIdentifierDelegateWrapper(originalDelegate); |
| 82 | + } |
| 83 | + |
| 84 | + private PostInsertIdentityPersister proxyPersister(final PostInsertIdentityPersister persister) |
| 85 | + { |
| 86 | + return (PostInsertIdentityPersister) Proxy.newProxyInstance( |
| 87 | + persister.getClass().getClassLoader(), |
| 88 | + ReflectionHacks.getAllInterfaces(persister), |
| 89 | + new InvocationHandler() |
| 90 | + { |
| 91 | + @Override |
| 92 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
| 93 | + { |
| 94 | + if ("getIdentifierType".equals(method.getName()) && (args == null || args.length == 0)) { |
| 95 | + // Override the getIdentifierType method |
| 96 | + // Return a primitive long type instead of the custom type |
| 97 | + return Objects.requireNonNull(primitiveType, "primitiveType must not be null"); |
| 98 | + } |
| 99 | + |
| 100 | + // For all other methods, delegate to the original persister |
| 101 | + return method.invoke(persister, args); |
| 102 | + } |
| 103 | + } |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + @SuppressWarnings("deprecation") |
| 108 | + private final class InsertGeneratedIdentifierDelegateWrapper implements InsertGeneratedIdentifierDelegate |
| 109 | + { |
| 110 | + |
| 111 | + private final InsertGeneratedIdentifierDelegate delegate; |
| 112 | + |
| 113 | + public InsertGeneratedIdentifierDelegateWrapper(final InsertGeneratedIdentifierDelegate delegate) |
| 114 | + { |
| 115 | + this.delegate = delegate; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Object performInsert(final String insertSQL, final SharedSessionContractImplementor session, final Binder binder) |
| 120 | + { |
| 121 | + var idType = Objects.requireNonNull(objectBigIntIdType, "objectBigIntIdType must not be null"); |
| 122 | + |
| 123 | + return idType.wrapJdbcValue(delegate.performInsert(insertSQL, session, binder)); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Object performInsert(final PreparedStatementDetails insertStatementDetails, final JdbcValueBindings valueBindings, final Object entity, final SharedSessionContractImplementor session) |
| 128 | + { |
| 129 | + var idType = Objects.requireNonNull(objectBigIntIdType, "objectBigIntIdType must not be null"); |
| 130 | + |
| 131 | + return idType.wrapJdbcValue(delegate.performInsert(insertStatementDetails, valueBindings, entity, session)); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String prepareIdentifierGeneratingInsert(final String insertSQL) |
| 136 | + { |
| 137 | + return delegate.prepareIdentifierGeneratingInsert(insertSQL); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert(final SqlStringGenerationContext context) |
| 142 | + { |
| 143 | + return delegate.prepareIdentifierGeneratingInsert(context); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public TableInsertBuilder createTableInsertBuilder(final BasicEntityIdentifierMapping identifierMapping, final Expectation expectation, final SessionFactoryImplementor sessionFactory) |
| 148 | + { |
| 149 | + return delegate.createTableInsertBuilder(identifierMapping, expectation, sessionFactory); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public PreparedStatement prepareStatement(final String insertSql, final SharedSessionContractImplementor session) |
| 154 | + { |
| 155 | + return delegate.prepareStatement(insertSql, session); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments