Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:26:26+00:00 2026-06-10T15:26:26+00:00

I need to do versioning on (simple) Java object graphs stored in a document-oriented

  • 0

I need to do versioning on (simple) Java object graphs stored in a document-oriented database (MongoDB). For relational databases and Hibernate, I discovered Envers and am very amazed about the possibilities. Is there something similar that can be used with Spring Data Documents?

I found this post outlining the thoughts I had (and more…) about storing the object versions, and my current implementation works similar in that it stores copies of the objects in a separate history collection with a timestamp, but I would like to improve this to save storage space. Therefore, I think I need to implement both a “diff” operation on object trees and a “merge” operation for reconstructing old objects. Are there any libraries out there helping with this?

Edit:
Any experiences with MongoDB and versioning highly appreciated! I see most probably there won’t be a Spring Data solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:26:28+00:00Added an answer on June 10, 2026 at 3:26 pm

    We’re using a base entity (where we set the Id, creation + last change dates,…). Building upon this we’re using a generic persistence method, which looks something like this:

    @Override
    public <E extends BaseEntity> ObjectId persist(E entity) {
        delta(entity);
        mongoDataStore.save(entity);
        return entity.getId();
    }
    

    The delta method looks like this (I’ll try to make this as generic as possible):

    protected <E extends BaseEntity> void delta(E newEntity) {
    
        // If the entity is null or has no ID, it hasn't been persisted before,
        // so there's no delta to calculate
        if ((newEntity == null) || (newEntity.getId() == null)) {
            return;
        }
    
        // Get the original entity
        @SuppressWarnings("unchecked")
        E oldEntity = (E) mongoDataStore.get(newEntity.getClass(), newEntity.getId()); 
    
        // Ensure that the old entity isn't null
        if (oldEntity == null) {
            LOG.error("Tried to compare and persist null objects - this is not allowed");
            return;
        }
    
        // Get the current user and ensure it is not null
        String email = ...;
    
        // Calculate the difference
        // We need to fetch the fields from the parent entity as well as they
        // are not automatically fetched
        Field[] fields = ArrayUtils.addAll(newEntity.getClass().getDeclaredFields(),
                BaseEntity.class.getDeclaredFields());
        Object oldField = null;
        Object newField = null;
        StringBuilder delta = new StringBuilder();
        for (Field field : fields) {
            field.setAccessible(true); // We need to access private fields
            try {
                oldField = field.get(oldEntity);
                newField = field.get(newEntity);
            } catch (IllegalArgumentException e) {
                LOG.error("Bad argument given");
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                LOG.error("Could not access the argument");
                e.printStackTrace();
            }
            if ((oldField != newField)
                    && (((oldField != null) && !oldField.equals(newField)) || ((newField != null) && !newField
                            .equals(oldField)))) {
                delta.append(field.getName()).append(": [").append(oldField).append("] -> [")
                        .append(newField).append("]  ");
            }
        }
    
        // Persist the difference
        if (delta.length() == 0) {
            LOG.warn("The delta is empty - this should not happen");
        } else {
            DeltaEntity deltaEntity = new DeltaEntity(oldEntity.getClass().toString(),
                    oldEntity.getId(), oldEntity.getUuid(), email, delta.toString());
            mongoDataStore.save(deltaEntity);
        }
        return;
    }
    

    Our delta entity looks like that (without the getters + setters, toString, hashCode, and equals):

    @Entity(value = "delta", noClassnameStored = true)
    public final class DeltaEntity extends BaseEntity {
        private static final long serialVersionUID = -2770175650780701908L;
    
        private String entityClass; // Do not call this className as Morphia will
                                // try to work some magic on this automatically
        private ObjectId entityId;
        private String entityUuid;
        private String userEmail;
        private String delta;
    
        public DeltaEntity() {
            super();
        }
    
        public DeltaEntity(final String entityClass, final ObjectId entityId, final String entityUuid,
                final String userEmail, final String delta) {
            this();
            this.entityClass = entityClass;
            this.entityId = entityId;
            this.entityUuid = entityUuid;
            this.userEmail = userEmail;
            this.delta = delta;
        }
    

    Hope this helps you getting started 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Java project I need to add versioning control to the files(Like SVN),
For a project I'm currently working on I need to implement object versioning. Unfortunately
I like an idea of document oriented databases like CouchDB. I am looking for
I need to serialize some data to string. The string is then stored in
A customer need a document managment system and I'm building information about this. I
i need help regarding dokuwiki i want to remove versioning from wiki. any tip
I'm working on versioning our database and am now searching for a way to
I need to build a simple content repository, but I need to be able
I don't understand, why i need versioning in my IPhone apps.Could anyone explain.
I need a value that is unique even when versioning is turned on in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.