【问题标题】:Spring Data Mongodb - Using @DocumentReference and @Filed at the same timeSpring Data Mongodb - 同时使用@DocumentReference 和@Filed
【发布时间】:2022-01-14 17:58:39
【问题描述】:

我想使用 @DocumentReference 和基本的 @Field 注释作为两个属性指向相同的属性(在 mongoDB 中),以便轻松操作它。为了实现这一点,我编写了自定义查找,但问题是,SpEL 中的 #self 为 null 我收到一个错误:

org.springframework.expression.spel.SpelEvaluationException: EL1007E: 在 null 上找不到属性或字段“project_id”

代码:

public record ProjectUser(@Id UUID id,
                      @Field("contact_email") String contactEmail,
                      @Field("phone") String phone,
                      @Field("company") String company,
                      @Field("job_title") String jobTitle,
                      @Field("note") String note,
                      @Field("parent_id") UUID parentId,
                      @Field("user_id") UUID userId,
                      @Field("project_id") UUID projectId,
                      @ReadOnlyProperty @DocumentReference(lookup = "{ '_id' : '?#{#self.project_id}' }") Project project,
                      @Field("project_role_id") UUID roleId,
                      @Field("project_group_ids") List<UUID> projectGroupIds,
                      @Field("deleted") Boolean deleted,
                      @Field("created_date") @CreatedDate OffsetDateTime createdDate,
                      @Field("updated_date") @LastModifiedDate OffsetDateTime updatedDate) {

}

有没有办法同时使用 @ReadOnlyProperty @DocumentReference(...) Project project@Field("project_id") UUID projectId ?非常感谢!!!

【问题讨论】:

    标签: java spring spring-boot spring-data spring-data-mongodb


    【解决方案1】:

    我对 kotlin 也有同样的问题,我认为这与我的情况下 spring 数据如何初始化引用有关

    不工作:

    @Document("test")
    data class Test(
      @Id
      val id: ObjectId? = null,
      val name: String,
      @CreatedDate
      val createdAt: Instant = Instant.MIN,
      @CreatedBy
      val createdById: ObjectId = ObjectId(),
      @ReadOnlyProperty
      @DocumentReference(lookup = "{ '_id' : ?#{#self.createdById} }")
      val createdBy: User? = null
    )
    

    工作:

    @Document("test")
    data class Test(
      @Id
      val id: ObjectId? = null,
      val name: String,
      @CreatedDate
      val createdAt: Instant = Instant.MIN,
      @CreatedBy
      val createdById: ObjectId = ObjectId(),      
    ) {
      @ReadOnlyProperty
      @DocumentReference(lookup = "{ '_id' : ?#{#self.createdById} }")
      var createdBy: User? = null
    }
    

    您可以尝试使用常规的 java 类而不是记录。

    【讨论】:

      猜你喜欢
      • 2022-11-13
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多