如何确定扩展抽象类的对象的类

时间:2018-06-12 02:58:13

标签: java jpa

我正在尝试在抽象类中放置JPA实体的通用功能:

@MappedSuperclass
public abstract class AbstractEntity implements java.io.Serializable {
    private static final Logger log = LoggerFactory.getLogger(ConcreteClass.class);

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private UUID uuid;

@Override
public int hashCode() {
    return getUuid().hashCode();
}
@Override
public boolean equals(final Object obj) {
    if (this == obj)
        return true;
    if (!(obj instanceof ConcreteClass))
        return false;
    return getUuid().equals(((ConcreteClass) obj).getUuid());
}

这可行/有用吗? Abstract类如何找出具体的类?

引用以下评论(工作代码):

@Override
public boolean equals(final Object obj) {
    if (this == obj)
        return true;
    if (obj instanceof AbstractEntity && this.getUuid().equals(((AbstractEntity)obj).getUuid()))
        return true;
    return false;
}

1 个答案:

答案 0 :(得分:1)

你不是。你真正关心的是

obj instanceof AbstractEntity && this.getUuid().equals(((AbstractEntity)obj).getUuid())

也许

this.getClass() == obj.getClass()