• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

181
Views
Where is `_class` built in Spring Data Couchbase when using 'findBy' methods?

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

To elaborate....

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 {
}

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());

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"    
}

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" 

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"
about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

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>

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

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

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error