简体   繁体   中英

Overwritten hashCode,equals and get/set- methods necessary in a JPA Entity?

I have a question to JPA. Is it absolutly necessary to create a overwritten hashCode method and a overwritten equals method like this:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    UserContent other = (UserContent) obj;
    if (id == null) {
        return false;
    } else if (!id.equals(other.id)) {
        return false;
    }
    return true;
}

Also i would like to know, if get- and set methods must be implemented. Or can they leave out and in which scenarios it can be leave out?

Are a method like this also necessary too?

@Override
public boolean isNew() {
    return this.id == null;
}

I use EclipseLink as JPA provider.

Thanks a lot! Maik

Overriding hashCode() and equals() is important for entities that are part of collections. It is not strictly required for other entities, but it is good practice to override them systematically, so you don't forget to add them later when you add new relationships.

Setters and getters are not mandatory. But again, it is good practice to make all your instance fields private and access them through public getters and setters.

As for the isNew() method, it is just a common utility method and is not at all required.

It depends and taking from JPA2.0 specs.

The primary key class must define equals and hashCode methods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped

If an embeddable class is used as a map key, the embeddable class must implement the hashCode and equals methods consistently with the database columns to which the embeddable is mapped

About getter and setters:

It is required that the entity class follow the method signature conventions for JavaBeans read/write properties (as defined by the JavaBeans Introspector class) for persistent properties when property access is used.

At the end only keys are required to override equals and hashcode, entities should but is not required, talking about getters and setters are only required if use property access, but remember that fields on entities can not be public so if you don't have getters and setters how you will set or get the properties.

The instance variables of a class must be private, protected, or package visibility independent of whether field access or property access is used. When property access is used, the property accessor methods must be public or protected

Actually keys marked with @Id should not have setters to avoid set manually that info. Obviously if you use the entity in collections you should consider override them to make good use of collection stuff.

About method isNew I never heard something about it, so it is not required and actually JPA not use it never in the specs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM