:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:


== Example Usage

For example:

[source,java]
----
@RequiredArgsConstructor(staticName = "of")
public static class ComplexNumber implements Comparable<ComplexNumber> {

    @Getter private final int real;
    @Getter private final int imaginary;

    private ObjectContracts.ObjectContract<ComplexNumber> contract
            = ObjectContracts.contract(ComplexNumber.class)
                .thenUse("real", ComplexNumber::getReal)
                .thenUse("imaginary", ComplexNumber::getImaginary);


    @Override
    public boolean equals(Object o) {
        return contract.equals(this, o);
    }

    @Override
    public int hashCode() {
        return contract.hashCode(this);
    }

    @Override
    public int compareTo(final ComplexNumber other) {
        return contract.compare(this, other);
    }

    @Override
    public String toString() {
        return contract.toString(this);
    }
}
----

[WARNING]
====
There are a number of deprecated methods that identify property names as strings.
These should _not_ be use, as they use are not type-safe and also use reflection heavily and so impose a performance hit.
====

== Usage Notes


=== Be aware of ORM loading issues

`ObjectContracts` implementation can cause DataNucleus to recursively rehydrate a larger number of associated entities (More detail below).

We therefore recommend that you disable
xref:refguide:config:sections/datanucleus.adoc#datanucleus.persistence-by-reachability-at-commit[persistence-by-reachability] by adding:

[source,ini]
.application.properties
----
datanucleus.persistenceByReachabilityAtCommit=false
----

[plantuml]
----
hide empty members

class Party
class AgreementRole
class Agreement

Party <-r-> "0.*" AgreementRole
AgreementRole "0..*" <-r-> Agreement
----

In the course of a transaction, the `Agreement` entity is loaded into memory (not necessarily modified), and then new ``AgreementRole``s are associated to it.

All these entities implement `Comparable` using `ObjectContracts`, so that the implementation of ``AgreementRole``'s (simplified) is:

[source,java]
----
public class AgreementRole {
    ...
    public int compareTo(AgreementRole other) {
        return ObjectContracts.compareTo(this, other, "agreement","startDate","party");
    }
    ...
}
----

while ``Agreement``'s is implemented as:

[source,java]
----
    public class Agreement {
        ...
        public int compareTo(Agreement other) {
            return ObjectContracts.compareTo(this, other, "reference");
        }
        ...
    }
----

and ``Party``'s is similarly implemented as:

[source,java]
----
public class Party {
    ...
    public int compareTo(Party other) {
        return ObjectContracts.compareTo(this, other, "reference");
    }
    ...
}
----

DataNucleus's persistence-by-reachability algorithm adds the ``AgreementRole``s into a `SortedSet`, which causes `AgreementRole#compareTo()` to fire:

* the evaluation of the "agreement" property delegates back to the `Agreement`, whose own `Agreement#compareTo()` uses the scalar `reference` property.
As the `Agreement` is already in-memory, this does not trigger any further database queries

* the evaluation of the "startDate" property is just a scalar property of the `AgreementRole`, so will already in-memory

* the evaluation of the "party" property delegates back to the `Party`, whose own `Party#compareTo()` requires the uses the scalar `reference` property.
However, since the `Party` is not yet in-memory, using the `reference` property triggers a database query to "rehydrate" the `Party` instance.

In other words, figuring out whether `AgreementRole` is comparable requires the persistence-by-reachability algorithm to run, causing the adjacent associated entity `Party` to also be retrieved.
