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

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

  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  • Compliance Automated Standard Solution (COMPASS), Part 10: How OSCAL Mapping Paves the Way for Continuous Compliance Scalability
  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents
  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
4.1K 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.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook