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
Newsletter
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

  • Part 2: Securing and Scaling Goose-to-Java Agent Traffic With agentgateway
  • Why Your AI Agent Works in the Demo and Fails in Production
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Spring Data Couchbase: Handle Unknown Class

Spring Data Couchbase: Handle Unknown Class

By 
Pavel Bernshtam
·
Jun. 22, 15 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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