DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Less Code With Spring Data Rest
  • Calling Stored Procedures With IN and OUT Parameters From Spring Data JPA
  • Spring Data: Easy MongoDB Migration Using Mongock

Trending

  • Build Retrieval-Augmented Generation (RAG) With Milvus
  • Java 23: What Developers Need to Know
  • Boosting Efficiency: Implementing Natural Language Processing With AWS RDS Using CloudFormation
  • 12 Expert Tips for Secure Cloud Deployments
  1. DZone
  2. Data Engineering
  3. Data
  4. Spring Data Couchbase: Handle Unknown Class

Spring Data Couchbase: Handle Unknown Class

By 
Pavel Bernshtam user avatar
Pavel Bernshtam
·
Jun. 22, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
3.6K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Data Couchbase  provides transparent way to save and load Java classes to and from Couchbase.

However, if a loaded class contains a property of unknown class, you will receive org.springframework.data.mapping.model.MappingException: No mapping metadata found for java.lang.Object

This may happen if, for example, different versions of your code save and load information.

In order to handle situation when we want to load an object, which contains another object on unknown class (in a map or list property) we should override the default SPMappingCouchbaseConverter.

Let's see how we do this with Spring XML configuration:

I replace my old XML:


<couchbase:couchbase id="cache" host="${couchbase.host}" bucket="mycache" password="password"/>
<couchbase:template id="cache-template" client-ref="cache"/>
<couchbase:repositories base-package="com.mycompany.cache" couchbase-template-ref="cache-template"/>


to the following XML:

<bean id="mappingContext"
          class="org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext"/>
<bean id="converter"
          class="com.mycompany.MyMappingCouchbaseConverter">
        <constructor-arg name="mappingContext" ref="mappingContext"/>
</bean>

<couchbase:couchbase id="cache" host="${couchbase.host}" bucket="mycache" password="password"/>
<bean id="cache-template" class="org.springframework.data.couchbase.core.CouchbaseTemplate">
        <constructor-arg name="client" ref="cache"/>
        <constructor-arg name="couchbaseConverter" ref="converter"/>
        <constructor-arg name="translationService">
            <null/>
        </constructor-arg>
 </bean>


<couchbase:repositories base-package="com.mycompany.cache" couchbase-template-ref="cache-template"/>


And create the following class:


public class MyMappingCouchbaseConverter extends MappingCouchbaseConverter {

    public MyMappingCouchbaseConverter(final MappingContext<? extends CouchbasePersistentEntity<?>,            CouchbasePersistentProperty> mappingContext) {
        super(mappingContext);
    }

    @Override
    protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source, final Object parent) {
        if (Object.class == typeMapper.readType(source, type).type) {
            return null;
        }

        return super.read(type, source, parent);
    }
}

Now, if loaded object will contain a property of unknown class or an object of unknown class in a list or map, this property or object will be replaced by null.



view source

print?
Spring Data Spring Framework Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Less Code With Spring Data Rest
  • Calling Stored Procedures With IN and OUT Parameters From Spring Data JPA
  • Spring Data: Easy MongoDB Migration Using Mongock

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: