使用“findBy"方法时,Spring Data Couchbase 中的“_class"在哪里? [英] Where is `_class` built in Spring Data Couchbase when using 'findBy' methods?

查看:35
本文介绍了使用“findBy"方法时,Spring Data Couchbase 中的“_class"在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以扩展/覆盖 Spring Data 中的哪些类以使 where 子句包含继承基本实体/模型的类?

Which classes in Spring Data can I extend/override to have the where-clause include the classes that inherrit the base entity/model?

详细说明....

以下是一些从基本"Activity 类扩展而来的模型:

Here are a few models that extends from a "base" Activity class:

@Document
public abstract class Activity {
}

@Document
public class CommentActivity extends Activity {
}

@Document
public class ShareActivity extends Activity {
}

保存这些模型时,我使用了一个单一的存储库类,如下面定义的findBy"方法:

When saving these models, I am using a single repository class as define below with a "findBy" method:

@Repository("activityRepository")
public interface Activity extends CouchbaseRepository<Activity, String>, CouchbaseRepositoryCustom<Activity, String> {

    Page<Activity> findByPersonId(String personId, Pageable pageable);

}

// saving a model in my application would be like so
activityRepository.save(new CommentActivity());

当从 Couchbase 取回它时,返回的结构是这样的:

When retrieving this back from Couchbase, the structure is returned like so:

{
    "_CAS": 123234342354345,
    "_ID": "0001",
    "_class": "com.myproject.models.CommentActivity",
    "comment": "yo",
    "dateCreated": 1400000000000,
    "dateUpdated": 1400000000000,
    "personId": "0001"    
}

该项目使用的是 Spring Data Couchbase 2.2.0.RELEASE.基于上面的 Repository 接口,调用 findByPersonId 方法将自动生成一个只包含 where 子句中的基类的查询.

The project is using Spring Data Couchbase 2.2.0.RELEASE. Based on the Repository Interface above, calling the findByPersonId method would auto-generate a query that only contains the base class in the where-clause.

WHERE (`personId` = "0001") AND `_class` = "com.myproject.models.Activity" 

就我而言,我想包括从 Activity 扩展的所有类型,如下所示:

In my case, I would like to include all types that extends from Activity like so:

   WHERE 
          (`_class` = "com.myproject.models.Activity" 
             OR `_class` = "com.myproject.models.UpdatedActivity" 
             OR `_class` = "com.myproject.models.StatusChangedActivity") 
   AND personId = "0001"

推荐答案

您可以定义自己的查询字符串如

You can define your own query string such as

@Query("#{#n1ql.selectEntity} WHERE personId = ${'$'}personId AND (_class='com.myproject.models.UpdatedActivity' OR _class='com.myproject.models.StatusChangedActivity'")
fun findByPersonId(@Param("personId") personId: String): List<Activity>

上面的代码是用 Kotlin 写的,但我想很容易适应 Java.

The above code is in Kotlin, but I guess easily adaptable to Java.

请注意,我必须将 @N1qlPrimaryIndexed 添加到存储库类.

Please note that I had to add @N1qlPrimaryIndexed to the repository class.

这篇关于使用“findBy"方法时,Spring Data Couchbase 中的“_class"在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆