lombok equalsandhashcode与继承

y53ybaqx  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(474)

我想用lombok:equalsandhashcode considering 唯一的id,设置在item类上,位于层次结构之上。以下是我的课程:1.流程:

@Data
@Entity
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@ToString(callSuper = true)
public class Process extends ResourceWithFlows {

  @OneToOne
  private Cost cost;
  private UUID referenceProduct; //flowBaseId
  private UUID processBaseId;
  private UUID processTemplateId;
  private boolean paretoApplied;

  //ref
  @ManyToOne
  @JoinColumn(name = "fk_equipment")
  private Equipment equipment;

  @ManyToOne
  @JoinColumn(name = "fk_production_line")
  private ProductionLine productionLine;

  @ManyToOne
  @JoinColumn(name = "fk_company")
  private Company company;

  @ManyToOne
  @JoinColumn(name = "fk_supply_chain")
  private SupplyChain supplyChain;

}

2.资源流动:

@Data
@Entity
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@ToString(callSuper = true)
public class ResourceWithFlows extends Resource {
    @OneToMany
    @JoinColumn(name = "fk_flow")
    private Set<Flow> flows;
    @OneToMany
    @JoinColumn(name = "fk_elementary_flow")
    private Set<ElementaryFlow> elementaryFlows;

}

3.项目:

@Data
@Entity
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Item {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @EqualsAndHashCode.Include
    private UUID id;
    private String name;
    private LocalDateTime creationDate;
    @Lob
    private String description;
    @OneToOne
    private AuthUser creatorUser;

    @PrePersist
    void creationDate () {
        this.creationDate = LocalDateTime.now();;
    }

}

是正确的还是有其他方法可以通过id来比较类项流程的子级?我在children中使用callsuper=true和onlyexplicitlyincluded=true,在父项的id属性上设置@equalsandhashcode.include,但我不确定它是否正确。

cidc1ykv

cidc1ykv1#

你所做的确实有用,但考虑到你的要求,似乎完全没有必要。如果你只想用 id 字段,无需申请 @EqualsAndHashCode 在等级制度下到处都是。中生成的方法 Item 将适用于所有子类。

相关问题