diff --git a/docs/src/main/asciidoc/mongodb-panache.adoc b/docs/src/main/asciidoc/mongodb-panache.adoc index c89ba5c262c858..8791e74d75fecc 100644 --- a/docs/src/main/asciidoc/mongodb-panache.adoc +++ b/docs/src/main/asciidoc/mongodb-panache.adoc @@ -669,7 +669,7 @@ Projection can be done for both PanacheQL and native queries. [source,java] ---- -import io.quarkus.mongodb.panache.ProjectionFor; +import io.quarkus.mongodb.panache.common.ProjectionFor; import org.bson.codecs.pojo.annotations.BsonProperty; // using public fields diff --git a/extensions/panache/mongodb-panache-common/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/BasePanacheMongoResourceProcessor.java b/extensions/panache/mongodb-panache-common/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/BasePanacheMongoResourceProcessor.java index 6dc98c5c6137d0..d08dddfac643a7 100644 --- a/extensions/panache/mongodb-panache-common/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/BasePanacheMongoResourceProcessor.java +++ b/extensions/panache/mongodb-panache-common/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/BasePanacheMongoResourceProcessor.java @@ -69,9 +69,11 @@ public abstract class BasePanacheMongoResourceProcessor { public static final DotName BSON_ID = createSimple(BsonId.class.getName()); public static final DotName BSON_IGNORE = createSimple(BsonIgnore.class.getName()); public static final DotName BSON_PROPERTY = createSimple(BsonProperty.class.getName()); - public static final DotName MONGO_ENTITY = createSimple(MongoEntity.class.getName()); + public static final DotName MONGO_ENTITY = createSimple(io.quarkus.mongodb.panache.common.MongoEntity.class.getName()); + public static final DotName DEPRECATED_MONGO_ENTITY = createSimple(MongoEntity.class.getName()); public static final DotName OBJECT_ID = createSimple(ObjectId.class.getName()); - public static final DotName PROJECTION_FOR = createSimple(ProjectionFor.class.getName()); + public static final DotName PROJECTION_FOR = createSimple(io.quarkus.mongodb.panache.common.ProjectionFor.class.getName()); + public static final DotName DEPRECATED_PROJECTION_FOR = createSimple(ProjectionFor.class.getName()); @BuildStep public void buildImperative(CombinedIndexBuildItem index, @@ -223,6 +225,27 @@ protected void handleProjectionFor(CombinedIndexBuildItem index, transformers.produce(new BytecodeTransformerBuildItem(info.name().toString(), fieldEnhancer)); } + // Register for building the property mapping cache + propertyMappingClass + .produce(new PropertyMappingClassBuildStep(targetClass.name().toString(), + annotationInstance.target().asClass().name().toString())); + } + for (AnnotationInstance annotationInstance : index.getIndex().getAnnotations(DEPRECATED_PROJECTION_FOR)) { + Type targetClass = annotationInstance.value().asClass(); + ClassInfo target = index.getIndex().getClassByName(targetClass.name()); + Map classPropertyMapping = new HashMap<>(); + extractMappings(classPropertyMapping, target, index); + propertyMapping.put(targetClass.name(), classPropertyMapping); + } + for (AnnotationInstance annotationInstance : index.getIndex().getAnnotations(DEPRECATED_PROJECTION_FOR)) { + Type targetClass = annotationInstance.value().asClass(); + Map targetPropertyMapping = propertyMapping.get(targetClass.name()); + if (targetPropertyMapping != null && !targetPropertyMapping.isEmpty()) { + ClassInfo info = annotationInstance.target().asClass(); + ProjectionForEnhancer fieldEnhancer = new ProjectionForEnhancer(targetPropertyMapping); + transformers.produce(new BytecodeTransformerBuildItem(info.name().toString(), fieldEnhancer)); + } + // Register for building the property mapping cache propertyMappingClass .produce(new PropertyMappingClassBuildStep(targetClass.name().toString(), @@ -242,6 +265,13 @@ public void mongoClientNames(ApplicationArchivesBuildItem applicationArchivesBui values.add(clientName.asString()); } } + instances = indexView.getAnnotations(DEPRECATED_MONGO_ENTITY); + for (AnnotationInstance annotation : instances) { + AnnotationValue clientName = annotation.value("clientName"); + if ((clientName != null) && !clientName.asString().isEmpty()) { + values.add(clientName.asString()); + } + } for (String value : values) { // we don't want the qualifier @MongoClientName qualifier added // as these clients will only be looked up programmatically via name diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/MongoEntity.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/MongoEntity.java index 1c07ba377c25c4..ff0a222fa1a053 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/MongoEntity.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/MongoEntity.java @@ -7,9 +7,12 @@ /** * This annotation can be used to specify some configuration of the mapping of an entity to MongoDB. + * + * @deprecated use {@link io.quarkus.mongodb.panache.common.MongoEntity} instead. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) +@Deprecated(forRemoval = true, since = "2.1.0") public @interface MongoEntity { /** * The name of the collection (if not set the name of the entity class will be used) diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/ProjectionFor.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/ProjectionFor.java index 260267b716cb5f..f51e84b674a820 100644 --- a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/ProjectionFor.java +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/ProjectionFor.java @@ -6,9 +6,13 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * @deprecated use {@link io.quarkus.mongodb.panache.common.ProjectionFor} instead. + */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) +@Deprecated(forRemoval = true, since = "2.1.0") public @interface ProjectionFor { Class value(); } diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/MongoEntity.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/MongoEntity.java new file mode 100644 index 00000000000000..59902dcc0e69e9 --- /dev/null +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/MongoEntity.java @@ -0,0 +1,29 @@ +package io.quarkus.mongodb.panache.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation can be used to specify some configuration of the mapping of an entity to MongoDB. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface MongoEntity { + /** + * The name of the collection (if not set the name of the entity class will be used) + */ + String collection() default ""; + + /** + * The name of the database (if not set the default from the property + * quarkus.mongodb.database will be used). + */ + String database() default ""; + + /** + * The name of the MongoDB client (if not set the default client will be used). + */ + String clientName() default ""; +} diff --git a/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/ProjectionFor.java b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/ProjectionFor.java new file mode 100644 index 00000000000000..5daf4937721109 --- /dev/null +++ b/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/ProjectionFor.java @@ -0,0 +1,14 @@ +package io.quarkus.mongodb.panache.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface ProjectionFor { + Class value(); +}