Skip to content

Commit 5bfc68e

Browse files
committed
add custom ImmutableType base class
1 parent a9418e3 commit 5bfc68e

File tree

14 files changed

+1559
-791
lines changed

14 files changed

+1559
-791
lines changed
Lines changed: 6 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,26 @@
11
package org.framefork.typedIds.bigint.hibernate;
22

3-
import io.hypersistence.utils.hibernate.type.ImmutableType;
4-
import io.hypersistence.utils.hibernate.type.util.ParameterizedParameterType;
53
import org.framefork.typedIds.bigint.ObjectBigIntId;
6-
import org.framefork.typedIds.bigint.ObjectBigIntIdTypeUtils;
4+
import org.framefork.typedIds.common.hibernate.ImmutableType;
5+
import org.framefork.typedIds.common.hibernate.ParameterizedTypeUtils;
76
import org.hibernate.HibernateException;
8-
import org.hibernate.dialect.Dialect;
9-
import org.hibernate.engine.spi.SharedSessionContractImplementor;
10-
import org.hibernate.metamodel.model.domain.DomainType;
11-
import org.hibernate.query.BindableType;
127
import org.hibernate.type.SqlTypes;
13-
import org.hibernate.type.descriptor.java.MutabilityPlan;
14-
import org.hibernate.type.descriptor.java.MutabilityPlanExposer;
158
import org.hibernate.type.descriptor.jdbc.JdbcType;
16-
import org.hibernate.type.spi.TypeConfiguration;
17-
import org.hibernate.type.spi.TypeConfigurationAware;
18-
import org.hibernate.usertype.DynamicParameterizedType;
19-
import org.jspecify.annotations.NonNull;
209
import org.jspecify.annotations.Nullable;
2110

22-
import java.sql.PreparedStatement;
23-
import java.sql.ResultSet;
24-
import java.sql.SQLException;
25-
import java.util.Objects;
26-
import java.util.Properties;
27-
28-
public class ObjectBigIntIdType extends ImmutableType<ObjectBigIntId<?>> implements
29-
BindableType<ObjectBigIntId<?>>,
30-
DomainType<ObjectBigIntId<?>>,
31-
MutabilityPlanExposer<ObjectBigIntId<?>>,
32-
DynamicParameterizedType,
33-
TypeConfigurationAware
11+
public class ObjectBigIntIdType extends ImmutableType<ObjectBigIntId<?>, ObjectBigIntIdJavaType>
3412
{
3513

3614
public static final String NAME = "object-bigint-id";
3715

38-
private final ObjectBigIntIdJavaType javaTypeDescriptor = new ObjectBigIntIdJavaType();
39-
40-
@Nullable
41-
private JdbcType jdbcTypeDescriptor;
42-
43-
@Nullable
44-
private TypeConfiguration typeConfiguration;
45-
4616
public ObjectBigIntIdType()
4717
{
4818
this((JdbcType) null);
4919
}
5020

5121
public ObjectBigIntIdType(@Nullable final JdbcType longJdbcType)
5222
{
53-
super(ObjectBigIntIdTypeUtils.getObjectBigIntIdRawClass());
54-
this.jdbcTypeDescriptor = longJdbcType;
23+
super(new ObjectBigIntIdJavaType(), longJdbcType);
5524
}
5625

5726
public ObjectBigIntIdType(final Class<?> implClass)
@@ -63,23 +32,7 @@ public ObjectBigIntIdType(final Class<?> implClass)
6332
public ObjectBigIntIdType(final Class<?> implClass, @Nullable final JdbcType longJdbcType)
6433
{
6534
this(longJdbcType);
66-
67-
var parameters = new Properties();
68-
parameters.put(DynamicParameterizedType.PARAMETER_TYPE, new ParameterizedParameterType(implClass));
69-
this.setParameterValues(parameters);
70-
}
71-
72-
@Override
73-
public void setTypeConfiguration(final TypeConfiguration typeConfiguration)
74-
{
75-
this.typeConfiguration = typeConfiguration;
76-
}
77-
78-
@Nullable
79-
@Override
80-
public TypeConfiguration getTypeConfiguration()
81-
{
82-
return typeConfiguration;
35+
this.setParameterValues(ParameterizedTypeUtils.forClass(implClass));
8336
}
8437

8538
@Override
@@ -94,62 +47,10 @@ public int getSqlType()
9447
return SqlTypes.BIGINT;
9548
}
9649

97-
@Override
98-
public long getDefaultSqlLength(final Dialect dialect, final JdbcType jdbcType)
99-
{
100-
return javaTypeDescriptor.getDefaultSqlLength(dialect, jdbcType);
101-
}
102-
103-
@NonNull
104-
public JdbcType getJdbcType()
105-
{
106-
return Objects.requireNonNull(jdbcTypeDescriptor, "jdbcTypeDescriptor is not yet initialized");
107-
}
108-
109-
@Override
110-
public ObjectBigIntIdJavaType getExpressibleJavaType()
111-
{
112-
return javaTypeDescriptor;
113-
}
114-
115-
@Override
116-
public void setParameterValues(final Properties parameters)
117-
{
118-
javaTypeDescriptor.setParameterValues(parameters);
119-
if (jdbcTypeDescriptor == null && typeConfiguration != null) {
120-
jdbcTypeDescriptor = typeConfiguration.getJdbcTypeRegistry().getDescriptor(getSqlType());
121-
}
122-
}
123-
124-
@Override
125-
public Class<ObjectBigIntId<?>> returnedClass()
126-
{
127-
return javaTypeDescriptor.getJavaTypeClass();
128-
}
129-
130-
@Override
131-
public Class<ObjectBigIntId<?>> getBindableJavaType()
132-
{
133-
return returnedClass();
134-
}
135-
136-
@Nullable
137-
@Override
138-
public ObjectBigIntId<?> fromStringValue(@Nullable final CharSequence sequence)
139-
{
140-
return javaTypeDescriptor.fromString(sequence);
141-
}
142-
143-
@Override
144-
public MutabilityPlan<ObjectBigIntId<?>> getExposedMutabilityPlan()
145-
{
146-
return javaTypeDescriptor.getMutabilityPlan();
147-
}
148-
14950
public ObjectBigIntId<?> wrapJdbcValue(final Object value)
15051
{
15152
if (value instanceof Long longValue) {
152-
return javaTypeDescriptor.wrap(longValue, null);
53+
return getExpressibleJavaType().wrap(longValue, null);
15354
}
15455
if (value instanceof Number numberValue) {
15556
return wrapJdbcValue(numberValue.longValue());
@@ -161,29 +62,4 @@ public ObjectBigIntId<?> wrapJdbcValue(final Object value)
16162
throw new HibernateException("Could not convert '%s' to '%s'".formatted(value.getClass().getName(), getReturnedClass()));
16263
}
16364

164-
/**
165-
* the nullSafeGet() is delegated here in the {@link ImmutableType}
166-
*/
167-
@Nullable
168-
@Override
169-
protected ObjectBigIntId<?> get(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException
170-
{
171-
return getJdbcType().getExtractor(javaTypeDescriptor).extract(rs, position, session);
172-
}
173-
174-
/**
175-
* the nullSafeSet() is delegated here in the {@link ImmutableType}
176-
*/
177-
@Override
178-
protected void set(PreparedStatement st, @Nullable ObjectBigIntId<?> value, int index, SharedSessionContractImplementor session) throws SQLException
179-
{
180-
getJdbcType().getBinder(javaTypeDescriptor).bind(st, value, index, session);
181-
}
182-
183-
@Override
184-
public String toString()
185-
{
186-
return "%s backed by %s".formatted(javaTypeDescriptor, jdbcTypeDescriptor != null ? jdbcTypeDescriptor : "???");
187-
}
188-
18965
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private ObjectBigIntIdType toObjectBigIntIdType(final Type type)
6262

6363
private JdbcType toJdbcType(final ObjectBigIntIdType objectBigIntIdType)
6464
{
65-
return objectBigIntIdType.getJdbcType();
65+
return objectBigIntIdType.getJdbcType(null);
6666
}
6767

6868
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private ObjectBigIntIdType toObjectBigIntIdType(final Type type)
6060

6161
private JdbcType toJdbcType(final ObjectBigIntIdType objectBigIntIdType)
6262
{
63-
return objectBigIntIdType.getJdbcType();
63+
return objectBigIntIdType.getJdbcType(null);
6464
}
6565

6666
}

0 commit comments

Comments
 (0)