Can I use eclipselink to get the differences between two objects?

View: New views
5 Messages — Rating Filter:   Alert me  

Can I use eclipselink to get the differences between two objects?

by Magnus Heino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi.

Lets say I have

Foo foo = new Foo(2);
Foo foo2 = new Foo(3);

Foo is an @Entity mapped by eclipselink.

Can I somehow use components of eclipselink to compare foo and foo2, and get a list of differences?

--

/Magnus Heino
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: Can I use eclipselink to get the differences between two objects?

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There is no public API to directly do this.

If you have an object registered in an EclipseLink UnitOfWork (any entity managed by an EntityManager), you can get the current changes for it made in the current transaction.  (see UnitOfWork.getCurrentChanges()).

There is an API of Session.compareObjects(), that will return true or false if two objects match, but not a ChangeSet.

If you wish to go under the covers a little their is an API, DeferredChangeDetectionPolicy.createObjectChangeSetThroughComparison(), that will return an ObjectChangeSet listing the changes between the two objects.


Magnus Heino wrote:
Hi.

Lets say I have

Foo foo = new Foo(2);
Foo foo2 = new Foo(3);

Foo is an @Entity mapped by eclipselink.

Can I somehow use components of eclipselink to compare foo and foo2, and get
a list of differences?

--

/Magnus Heino

Re: Can I use eclipselink to get the differences between two objects?

by Magnus Heino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


If you wish to go under the covers a little their is an API,
DeferredChangeDetectionPolicy.createObjectChangeSetThroughComparison(), that
will return an ObjectChangeSet listing the changes between the two objects.

Is it possible to make it consider @OneToMany mappings also?

It will report a difference no matter what I put into the @OneToMany set, even if the objects themself are equal if passed into the  createObjectChangeSetThroughComparison method.

I would like to work with a object graph rather than with individual objects.

Under the covers api's are not a problem.

I currently do it like this:

    @Autowired
    private JpaEntityManager entityManager;

    private final ObjectChangePolicy objectChangePolicy = new DeferredChangeDetectionPolicy();

    public ObjectChangeSet compare(final AbstractEntity original, final AbstractEntity copy) {
        ObjectChangeSet objectChangeSet = this.objectChangePolicy.createObjectChangeSetThroughComparison(original, copy,
            (UnitOfWorkChangeSet) this.entityManager.getUnitOfWork().getCurrentChanges(), false, (AbstractSession) this.entityManager
                .getActiveSession(), this.entityManager.getActiveSession().getDescriptor(original.getClass()));

        return objectChangeSet;
    }

Thanks!

/Magnus




Magnus Heino wrote:
>
> Hi.
>
> Lets say I have
>
> Foo foo = new Foo(2);
> Foo foo2 = new Foo(3);
>
> Foo is an @Entity mapped by eclipselink.
>
> Can I somehow use components of eclipselink to compare foo and foo2, and
> get
> a list of differences?
>
> --
>
> /Magnus Heino
>


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink ,
http://wiki.oracle.com/page/TopLink TopLink
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
http://www.nabble.com/EclipseLink-f26430.html EclipseLink
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
--
View this message in context: http://www.nabble.com/Can-I-use-eclipselink-to-get-the-differences-between-two-objects--tp17753109p17756093.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.

_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



--

/Magnus Heino
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: Can I use eclipselink to get the differences between two objects?

by Magnus Heino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


This seems to be impossible since ContainerPolicy.compareCollectionsForChange that will be called by DeferredChangeDetectionPolicy to compare collections is using a IdentityHashMap. Correct?

/Magnus

On Tue, Jun 10, 2008 at 9:11 PM, Magnus Heino <magnus@...> wrote:

If you wish to go under the covers a little their is an API,
DeferredChangeDetectionPolicy.createObjectChangeSetThroughComparison(), that
will return an ObjectChangeSet listing the changes between the two objects.

Is it possible to make it consider @OneToMany mappings also?

It will report a difference no matter what I put into the @OneToMany set, even if the objects themself are equal if passed into the  createObjectChangeSetThroughComparison method.

I would like to work with a object graph rather than with individual objects.

Under the covers api's are not a problem.

I currently do it like this:

    @Autowired
    private JpaEntityManager entityManager;

    private final ObjectChangePolicy objectChangePolicy = new DeferredChangeDetectionPolicy();

    public ObjectChangeSet compare(final AbstractEntity original, final AbstractEntity copy) {
        ObjectChangeSet objectChangeSet = this.objectChangePolicy.createObjectChangeSetThroughComparison(original, copy,
            (UnitOfWorkChangeSet) this.entityManager.getUnitOfWork().getCurrentChanges(), false, (AbstractSession) this.entityManager
                .getActiveSession(), this.entityManager.getActiveSession().getDescriptor(original.getClass()));

        return objectChangeSet;
    }

Thanks!

/Magnus




Magnus Heino wrote:
>
> Hi.
>
> Lets say I have
>
> Foo foo = new Foo(2);
> Foo foo2 = new Foo(3);
>
> Foo is an @Entity mapped by eclipselink.
>
> Can I somehow use components of eclipselink to compare foo and foo2, and
> get
> a list of differences?
>
> --
>
> /Magnus Heino
>


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink ,
http://wiki.oracle.com/page/TopLink TopLink
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
http://www.nabble.com/EclipseLink-f26430.html EclipseLink
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
--
View this message in context: http://www.nabble.com/Can-I-use-eclipselink-to-get-the-differences-between-two-objects--tp17753109p17756093.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.

_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



--

/Magnus Heino



--

/Magnus Heino
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: Can I use eclipselink to get the differences between two objects?

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It is using identity.  I order to make it work you would need it to resolve identity correctly for your objects.  Essentially if you create a new UnitOfWorkImpl and manufacture its' cloneMapping so that all of the objects you are comparing are the key and all of the same objects you are comparing with are the value, it may resolve correctly.

If you get it working, it may be a useful feature.  Perhaps log an enhancement for having some kind of change service in EclipseLink, and attach your code as an example.


Magnus Heino wrote:
This seems to be impossible since
ContainerPolicy.compareCollectionsForChange that will be called by
DeferredChangeDetectionPolicy to compare collections is using a
IdentityHashMap. Correct?

/Magnus

>
On Tue, Jun 10, 2008 at 9:11 PM, Magnus Heino <magnus@filur.org> wrote:

>
> If you wish to go under the covers a little their is an API,
>> DeferredChangeDetectionPolicy.createObjectChangeSetThroughComparison(),
>> that
>> will return an ObjectChangeSet listing the changes between the two
>> objects.
>>
>
> Is it possible to make it consider @OneToMany mappings also?
>
> It will report a difference no matter what I put into the @OneToMany set,
> even if the objects themself are equal if passed into the
> createObjectChangeSetThroughComparison method.
>
> I would like to work with a object graph rather than with individual
> objects.
>
> Under the covers api's are not a problem.
>
> I currently do it like this:
>
>     @Autowired
>     private JpaEntityManager entityManager;
>
>     private final ObjectChangePolicy objectChangePolicy = new
> DeferredChangeDetectionPolicy();
>
>     public ObjectChangeSet compare(final AbstractEntity original, final
> AbstractEntity copy) {
>         ObjectChangeSet objectChangeSet =
> this.objectChangePolicy.createObjectChangeSetThroughComparison(original,
> copy,
>             (UnitOfWorkChangeSet)
> this.entityManager.getUnitOfWork().getCurrentChanges(), false,
> (AbstractSession) this.entityManager
>                 .getActiveSession(),
> this.entityManager.getActiveSession().getDescriptor(original.getClass()));
>
>         return objectChangeSet;
>     }
>
> Thanks!
>
> /Magnus
>
>>
>
>>
>>
>> Magnus Heino wrote:
>> >
>> > Hi.
>> >
>> > Lets say I have
>> >
>> > Foo foo = new Foo(2);
>> > Foo foo2 = new Foo(3);
>> >
>> > Foo is an @Entity mapped by eclipselink.
>> >
>> > Can I somehow use components of eclipselink to compare foo and foo2, and
>> > get
>> > a list of differences?
>> >
>> > --
>> >
>> > /Magnus Heino
>> >