简体   繁体   中英

Unable to Implement Cutom Comparator for Javers Comparison

Hi I am new to Javers .

I have an Entity , for which I am implementing a custom comparator in order to compare using Javers .

My entity :-

package com.devyansh.entity;

import java.util.List;

import org.javers.core.metamodel.annotation.Entity;

@Entity
public class Ent {

    String value;
    List<String> values;
    public Ent(String value, List<String> values) {
        super();
        this.value = value;
        this.values = values;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public List<String> getValues() {
        return values;
    }
    public void setValues(List<String> values) {
        this.values = values;
    }


}

Below I am registering the new Comparator :-

public void comp(){

    Javers javers = JaversBuilder.javers().registerCustomComparator(new FunnyStringComparator(), Ent.class).build();

    Diff diff = javers.compare(new Ent("aaa", new ArrayList<String>()), new Ent("aaa",new ArrayList<String>()));

    System.out.println(diff.getChanges());
}

My Comparator Implementation :-

package com.devyansh.javers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import com.devyansh.entity.Ent;
import com.google.common.collect.Sets; 

import org.javers.core.diff.changetype.PropertyChangeMetadata;
import org.javers.core.diff.changetype.container.ContainerElementChange;
import org.javers.core.diff.changetype.container.SetChange;
import org.javers.core.diff.changetype.container.ValueAdded;
import org.javers.core.diff.changetype.container.ValueRemoved;
import org.javers.core.diff.custom.CustomPropertyComparator;
import org.javers.core.metamodel.property.Property;

public class FunnyStringComparator implements CustomPropertyComparator<Ent, SetChange> {

    @Override
    public Optional<SetChange> compare(Ent left, Ent right, PropertyChangeMetadata metadata,
            Property property) {
        // TODO Auto-generated method stub
        System.out.println("Hello!!!!!");


            return null ;


    }

    @Override
    public boolean equals(Ent a, Ent b) {
        // TODO Auto-generated method stub
        return false;
    }



}

PROBLEM AREA :-

WHENEVER I TRY TO COMPARE USING THE BELOW LINE :-

Diff diff = javers.compare(new Ent("aaa", new ArrayList()), new Ent("aaa",new ArrayList()));

I get the following error :-

MANAGED_CLASS_MAPPING_ERROR: given javaClass 'class com.devyansh.entity.Ent' is mapped to CustomType, expected ManagedType

Can anybody please let me know what I am doing wrong ??? I tried to debug the Javers api , below function is called :-

JaversType get(Type javaType) {
    return mappedTypes.get(javaType.toString());
}

here mappedTypes is a concurrentHashMap , and it reutrns a customType for user defined entities ?

How can we resolve this ?

Currently, CustomTypes objects are supported only when they are inside ObjectGraphs. When CustomTypes objects are passed directly to Javers.compare(), Javers fails on genertating GlobalId for them.

I have created the issue for this case https://github.com/javers/javers/issues/882 , contributions are welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM