ObjectDB ObjectDB

Implement "equals" method

#1

Will something unexpected happen, if I implement my own "equals" method for my entities? e.g. comparing my own UUID property

The problem is, that I have a Set and I have two entities from different DB requests and they fail to be equal (why actually?). Thus both of them are in the Set, where actually there should be only one (the same).

edit
delete
#2

ObjectDB does not use the equals methods of objects, so you can define it as you wish.

Mixing objects from different EntityManager instances, however, is a bad practice and should be avoided. Each EntityManager instance has its own representation of database objects so the same database object is represented by more than one Java object, and you should not have references between objects of different EntityManager instances.

ObjectDB Support
edit
delete
#3

I am having trouble using ObjectDB entity objects with ComboBox in Vaadin [1]. I have the same entry twice and so. I have implemented my own equals method, but still have this problem. How can this be? Are you maybe using proxies?

[1] https://vaadin.com/docs/latest/components/combo-box

edit
delete
#4

Okay, I also had to implement the Object.hashCode() method. I did it like this:


@Override

public boolean equals(java.lang.Object object) {
    if(this == object) return true;
    if(object == null) return false;
    if(this.getClass() != object.getClass()) return false;
    return Objects.equals(this.getUUID(), this.getClass().cast(object).getUUID());
}


@Override
public int hashCode() {
    return Objects.hashCode(this.getUUID());
}
edit
delete
#5

Yes, this is an important rule in Java to follow (not related to ObjectDB).

Hopefully this solves the issue, if not, check equals directly, not using a ComboBox.

ObjectDB Support
edit
delete

Reply

To post on this website please sign in.