0

I am trying to compare two VOs using Javers but however I am having some difficulties due to java.sql.Timestamp and java.util.Date field comparison shows there is change detected but no change has actually made to the fields at all!

Below is my Scenario:

@Entity
class A {
  @Id private long id;
  private SortedSet<B> objB;

  public SortedSet<B> getObjB() {
    return objB;
  }

  public void setObjB(SortedSet<B> objB) {
    this.objB = objB;
  }
}

@Entity
class B implements comparable<B> {
  @Id private long id;
  private java.util.Date startDate;
  private java.util.Date endDate;

  public Date getStartDate() {
    return startDate;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }


  public Date getEndDate() {
    return endDate;
  }

  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }

  equals(...) {
    ...
  }
  hashCode(...) {
    ...
  }
}

class JaversDateCompareDemo {
  public static void main(String[] args) {
    B b1= new B();
    b1.setStartDate(new java.sql.Timestamp(1559822652957));
    b1.setEndDate(new java.sql.Timestamp(1559822652957));

    SortedSet<B> s1 = new TreeSet();
    s1.add(b1);

    B b2 = new B();
    b2.setStartDate(1559822652957);
    b2.setEndDate(1559822652957);

    SortedSet<B> s2 = new TreeSet();
    s2.add(b2);

    A a1 = new A();
    a.setObjB(s1);

    A a2 = new A();
    a.setObjB(s2);

    Javers javers = JaversBuilder.javers().withListCompareAlgorithm(lca).build();
    Diff diff = javers.compare(a1, a2);
    System.out.println(diff.prettyPrint());
  }
}

Output:

Diff:
* changes on A/xxx :
  - 'objB/xxx.endDate' value changed from '2019-06-06 12:04:12.976' to 'Thu Jun 06 12:04:12 GMT 2019'
  - 'objB/xxx.startDate' value changed from '2019-06-06 12:04:12.976' to 'Thu Jun 06 12:04:12 GMT 2019'

My code base is actually very large and we want to have a standard way to compare and maintain the audit log of changes and the thing is, currently I am parsing the date fields from java.sql.Timestamp to java.util.Date manually where ever it is defined(which is very hectic).

So

  1. I want to know if there is any better way to avoid this kind of changes.
  2. Does Javers provide the feature to compare 2 Dates of different type(as mentioned above).
  3. If there is no such feature available currently with Javers can this feature be added to it. I think this would be very much helpful in this kind of scenarios.

Thanks.

1 Answer 1

1

JaVers uses equals() to compare Values, and looks like java.sql.Timestamp is not equal to java.util.Date even if they have the same value.

      given:
      Timestamp t = new Timestamp(1559822652957)
      Date d = new Date(1559822652957)

      when:
        println d
        println t

      println ('d.equals(t) ' + d.equals(t))
      println ('t.equals(d) ' + t.equals(d))

      then:
      t.time == d.time

output

Thu Jun 06 14:04:12 CEST 2019
2019-06-06 14:04:12.957
d.equals(t) true
t.equals(d) false
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Bartek for the timely response.
I see now Timestamp and Date equals() works quite differently as per javadoc. And this is exactly my scenario(t.equals(d)). Do you have any suggestion on what can be done in this case else will have to cast every single timestamp value to date. Thanks.
Try to write a CustomComparator and register it for both types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.