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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Navigating Double and Triple Extortion Tactics
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Designing for Sustainability: The Rise of Green Software
  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.9K 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
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!