I am using hibernate and id... is used for persistence (which is why it is omitted in comparison). (Also, using google guava helper equals)
HolidayPackageVariant:
public abstract class HolidayPackageVariant {
private Integer idHolidayPackageVariant;
private HolidayPackage holidayPackage;
private String typeHolidayPackage;
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if(obj == null)
return false;
if (getClass().equals(obj.getClass())) {
final HolidayPackageVariant otherPackageVariant = (HolidayPackageVariant) obj;
return Objects.equal(getTypeHolidayPackage(),otherPackageVariant.getTypeHolidayPackage())
&& Objects.equal(getHolidayPackage(),
otherPackageVariant.getHolidayPackage());
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(getTypeHolidayPackage(), getHolidayPackage());
}
FlightHolidayPackageVariant:
public final class FlightHolidayPackageVariant extends HolidayPackageVariant{
private Destination originCity;
public boolean equals(Object obj) {
// ..
Should I completely override the equals() or should I be invoking super.equals(...) in some way ?
Following the Secret of Equals:
HolidayPackageVariant:
FlightHolidayPackageVariant:
This will ensure that only the same Type of variants are equal to each other.
This is how you should implement data objects: no inheritance allowed, composition only.
These are immutable objects, but you could want to modify them, so remove
final
where needed. Also you may removefinal
from the class definition because Hibernate doesn't support it, but in that case, you should document that these classes are not eligible for inheritance.The main advantages are all the ones described in Effective Java plus, in this case, you don't have to manage inheritance through Hibernate which can sometimes be a real pain.