orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

OObjectLazySet duplicate entries

Open photoni opened this issue 13 years ago • 5 comments

Hi, I'm trying to store an object a of type A with a set of elements inside of type B.
hashcode and equals methods in my type B are defined as following @Override public int hashCode() {

    return 1;
}

@Override
public boolean equals(Object obj) {

    return true;
}

In my code(pseudo) I do the following: B b=new B(); A a=db.load(""); Set elements=a.getElements(); elements.add(b); db.save(a);

Running this test multiple times the db keep adding elements to the set although B identity semantic is saying that all B instances are identical.

Thanks

photoni avatar Jun 23 '13 16:06 photoni

@MaDaPHaKa have you any idea why it happens?

lvca avatar Jun 24 '13 09:06 lvca

Fixed in commit 4344328a658114c46938bd85a7b5229c3854974f (branch hotfix-1.4.2)

MaDaPHaKa avatar Jun 24 '13 12:06 MaDaPHaKa

Hi, there are still problems about set duplication. I'm commenting this closed issue rather than opening a new one and I let you decide whether reopening it or not.

I've downloaded your fix from the hotfix1.4.2 branch, compiled and installed in my local repo to replace the 1.4.1. I've replaced only the orientdb-object jar, the one containing the fixed class. com.orientechnologies orientdb-object 1.4.2

To simplify the description of the problem I've created two minimal versions of hashcode and equals. The semantic is: All the objects in the set are equals if they belong to the same class(a simplified version of the standard equals semantic).

@Override
public int hashCode() {

    return 1;
}   

@Override
public boolean equals(Object obj) {
    if (!this.getClass().equals(obj.getClass())) {
        log.debug(
                "Unexpected: returning false. this.getClass():{} - obj.getClass():{}",
                this.getClass(), obj.getClass());
        return false;
    }
    log.debug(
            "Expected: returning true. this.getClass():{} - obj.getClass():{}",
            this.getClass(), obj.getClass());
    return true;
}

This is what I get when I run the test the second time:

//The Class already contains a persisted element and the size of the lazySet is 1 //Then I add a newly created object to the lazy set //here I get the unexpected behavior. The incoming object (not proxied) is added to a set of underlying proxied objects and the equals fails when comparing classes DEBUG org.photoni.model.SyndFeedDocument.equals(SyndFeedDocument.java:138) Unexpected: returning false. this.getClass():class org.photoni.model.SyndFeedDocument - obj.getClass():class org.photoni.model.SyndFeedDocument_$$_javassist_1 //The new size of the lazy set is 2 DEBUG org.photoni.rss.RSSAggregator.aggregate(RSSAggregator.java:94) Persisting 2 entries for feed in set implementation com.orientechnologies.orient.object.db.OObjectLazySet

//here the already converted objects are added to another set, probably when flushing changes to the db. The method equals is called againg,this time with success, but too late

DEBUG org.photoni.model.SyndFeedDocument.equals(SyndFeedDocument.java:143) Expected: returning true. this.getClass():class org.photoni.model.SyndFeedDocument_$$javassist_1 - obj.getClass():class org.photoni.model.SyndFeedDocument$$javassist_1 DEBUG org.photoni.model.SyndFeedDocument.equals(SyndFeedDocument.java:143) Expected: returning true. this.getClass():class org.photoni.model.SyndFeedDocument$$javassist_1 - obj.getClass():class org.photoni.model.SyndFeedDocument$$_javassist_1

//the standard equals semantic is broken by javassist runtime proxyes

Regards

photoni avatar Jun 24 '13 23:06 photoni

Hi,

the only way to manage this would be to:

  1. check if the Set contains the non wrappet object,
  2. wrap around a proxy a non-proxied instance added, check if the set contains it
  3. if not add the original one (that will be proxied again when saved). All of that because wrap around the proxy the original instance and add it would mean that all modifications won't be replicated in the proxied one.

It seems to much overhead to me :) I think the best way is to use the db.newInstance() API or manage the proxied object case in the equals().

MaDaPHaKa avatar Jun 25 '13 16:06 MaDaPHaKa

Hi, I don't have any problem in managing it in my code, it's not blocking my work so far. I've posted the issue, because identity management is the foundation of a data modeling and coupling the code to the logic of your system can be a limitation in the adoption, especially by those architects addicted to JPA/Hibernate. It's a sin cause I think that your db is a fantastic product Thanks for your quick answer Regards

photoni avatar Jun 25 '13 18:06 photoni