This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 137907 - Change Dafault equals Code in Generated Entities
Summary: Change Dafault equals Code in Generated Entities
Status: RESOLVED WONTFIX
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Andrei Badea
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-06-22 20:36 UTC by cadorca
Modified: 2016-07-07 08:53 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description cadorca 2008-06-22 20:36:03 UTC
When the IDE generate a entity its generates somethign like this
@Override
    public int hashCode() {
        int hash = 0;
        hash += (productId != null ? productId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Product)) {
            return false;
        }
        Product other = (Product) object;
        if ((this.productId == null && other.productId != null) || (this.productId != null &&
!this.productId.equals(other.productId))) {
            return false;
        }
        return true;
    }

in this code create a TODO that warning that this code doesnt work with null primary key, I think that will  be very
useful if we change this default code for the next

The idea is that is one of the primaries keys is null them you should compare the object hashcode so:
// Is necessary to change the equals method
@Override
    public int hashCode() {
        int hash = (productId != null ? productId.hashCode() : super.hashCode());
        return hash;
    }

and then the equals code
@Override
    public boolean equals(Object object) {
        if (!(object instanceof Product)) {
            return false;
        }
        Product other = (Product) object;
        if (this.productId == null || other.productId == null) {
            return this.hashCode() == other.hashCode();
        } else {
            return this.productId.equals(other.productId);
        }
    }

I hope for the next release this can be changed and so i dont have to make this change for each generated entities.

Thanks.
Comment 1 Sergey Petrov 2009-12-11 03:46:29 UTC
suggested equal seems wrong to me, as hashCode equal shouldn't mean object equals. hashcode may be always constant in super or in Id class and it will case wrong true for suggested equal in case if at least one id is null.
also it's not clear what exactly do not work in case of null id.
Comment 2 Martin Balin 2016-07-07 08:53:34 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss