From eda71c9e8cea4aba3b08a134abd0961e438e6976 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Mon, 15 Jul 2024 15:59:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96jpa=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jpa/JpaEntityTableMetadataParser.java | 16 +++- ...JpaEntityTableMetadataParserProcessor.java | 96 ++++++++++--------- 2 files changed, 63 insertions(+), 49 deletions(-) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParser.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParser.java index ac07801c..58b45673 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParser.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParser.java @@ -2,6 +2,7 @@ import lombok.Setter; +import lombok.SneakyThrows; import org.hswebframework.ezorm.rdb.mapping.annotation.Comment; import org.hswebframework.ezorm.rdb.mapping.parser.*; import org.hswebframework.ezorm.rdb.metadata.RDBDatabaseMetadata; @@ -12,6 +13,8 @@ import javax.persistence.Table; import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.function.Supplier; /** * @see javax.persistence.Column @@ -20,8 +23,15 @@ */ public class JpaEntityTableMetadataParser implements EntityTableMetadataParser { - @Setter - private RDBDatabaseMetadata databaseMetadata; + private Callable databaseMetadata; + + public void setDatabaseMetadata(Callable databaseMetadata) { + this.databaseMetadata = databaseMetadata; + } + + public void setDatabaseMetadata(RDBDatabaseMetadata databaseMetadata) { + this.databaseMetadata = () -> databaseMetadata; + } @Setter private DataTypeResolver dataTypeResolver = DefaultDataTypeResolver.INSTANCE; @@ -30,12 +40,14 @@ public class JpaEntityTableMetadataParser implements EntityTableMetadataParser { private ValueCodecResolver valueCodecResolver = DefaultValueCodecResolver.COMMONS; + @SneakyThrows public Optional parseTableMetadata(Class entityType) { Table table = AnnotationUtils.getAnnotation(entityType, Table.class); if (table == null) { return Optional.empty(); } + RDBDatabaseMetadata databaseMetadata = this.databaseMetadata.call(); RDBSchemaMetadata schema = databaseMetadata .getSchema(table.schema()) .orElseGet(databaseMetadata::getCurrentSchema); diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParserProcessor.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParserProcessor.java index 8576a3c7..e773d739 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParserProcessor.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/mapping/jpa/JpaEntityTableMetadataParserProcessor.java @@ -109,13 +109,13 @@ public void process() { Set annotations = getAnnotations(entityType, descriptor); getAnnotation(annotations, Column.class) - .ifPresent(column -> handleColumnAnnotation(descriptor, annotations, ColumnInfo.of(column))); + .ifPresent(column -> handleColumnAnnotation(descriptor, annotations, ColumnInfo.of(column))); getAnnotation(annotations, JoinColumns.class) - .ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column.value()))); + .ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column.value()))); getAnnotation(annotations, JoinColumn.class) - .ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column))); + .ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column))); } @@ -170,13 +170,13 @@ private void handleJoinColumnAnnotation(PropertyDescriptor descriptor, Set builder.setAssociationType(AssociationType.oneToOne)); + .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToOne)); getAnnotation(annotations, OneToMany.class) - .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToMay)); + .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToMay)); getAnnotation(annotations, ManyToMany.class) - .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToMay)); + .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToMay)); getAnnotation(annotations, ManyToOne.class) - .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToOne)); + .ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToOne)); for (JoinColumn joinColumn : column) { @@ -256,8 +256,8 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set { - if (gen.strategy() == GenerationType.SEQUENCE) { - metadata.setAutoIncrement(true); - metadata.setProperty("seq_name", gen.generator()); - return null; - } else { - DefaultValueGenerator generator = LazyDefaultValueGenerator - .of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator()))); - return generator.generate(metadata); - } - }) - .ifPresent(metadata::setDefaultValue); + .map(gen -> { + if (gen.strategy() == GenerationType.SEQUENCE) { + metadata.setAutoIncrement(true); + metadata.setProperty("seq_name", gen.generator()); + return null; + } else { + DefaultValueGenerator generator = LazyDefaultValueGenerator + .of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator()))); + return generator.generate(metadata); + } + }) + .ifPresent(metadata::setDefaultValue); getAnnotation(annotations, DefaultValue.class) - .map(gen -> { - if (gen.value().isEmpty()) { - return LazyDefaultValueGenerator - .of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator()))) - .generate(metadata); - } - return (RuntimeDefaultValue) gen::value; - }) - .ifPresent(metadata::setDefaultValue); + .map(gen -> { + if (gen.value().isEmpty()) { + return LazyDefaultValueGenerator + .of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator()))) + .generate(metadata); + } + return (RuntimeDefaultValue) gen::value; + }) + .ifPresent(metadata::setDefaultValue); getAnnotation(annotations, Comment.class) - .map(Comment::value) - .ifPresent(metadata::setComment); + .map(Comment::value) + .ifPresent(metadata::setComment); getAnnotation(annotations, Upsert.class) - .map(Upsert::insertOnly) - .ifPresent(insertOnly -> metadata.setSaveable(!insertOnly)); + .map(Upsert::insertOnly) + .ifPresent(insertOnly -> metadata.setSaveable(!insertOnly)); getAnnotation(annotations, Id.class).ifPresent(id -> metadata.setPrimaryKey(true)); @@ -330,24 +330,24 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set resolver.resolve(propertyDescriptor)) - .ifPresent(metadata::setType); + .map(resolver -> resolver.resolve(propertyDescriptor)) + .ifPresent(metadata::setType); if (metadata.getType() == null) { tableMetadata - .getDialect() - .convertSqlType(metadata.getJavaType()) - .ifPresent(jdbcType -> metadata.setJdbcType(jdbcType, metadata.getJavaType())); + .getDialect() + .convertSqlType(metadata.getJavaType()) + .ifPresent(jdbcType -> metadata.setJdbcType(jdbcType, metadata.getJavaType())); } ofNullable(valueCodecResolver) - .map(resolver -> resolver.resolve(propertyDescriptor) - .orElseGet(() -> metadata - .findFeature(ValueCodecFactory.ID) - .flatMap(factory -> factory.createValueCodec(metadata)) - .orElse(null))) - .ifPresent(metadata::setValueCodec); - ; + .map(resolver -> resolver + .resolve(propertyDescriptor) + .orElseGet(() -> metadata + .findFeature(ValueCodecFactory.ID) + .flatMap(factory -> factory.createValueCodec(metadata)) + .orElse(null))) + .ifPresent(metadata::setValueCodec); if (metadata.getValueCodec() instanceof EnumValueCodec) { EnumValueCodec codec = ((EnumValueCodec) metadata.getValueCodec()); if (codec.isToMask()) { @@ -357,7 +357,9 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set