根据其值在JPA复杂对象中搜索嵌套对象

时间:2019-02-14 20:58:13

标签: java hibernate jpa graph

让我们假设我们有一个复杂的JPA关系,其中一部分看起来像这样:

@MappedSuperclass
public class DiffEntity {

  private String diffId;

  public DiffEntity() {
    this.diffId = UUID.randomUUID().toString();
  }
  //...
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class ParentEntity extends DiffEntity {

  @Id
  @GeneratedValue
  private long id;

  @Column
  private String name;

 //...
}

@Entity
public class Construct extends ParentEntity {

  @Column
  private String variable;

  @OneToMany(mappedBy = "construct", cascade = CascadeType.ALL)
  private List<Partconstruct> partconstructs;

 //...

 }

@Entity
public class Partconstruct extends ParentEntity {

  @OneToMany(mappedBy = "partconstruct", cascade = CascadeType.ALL)
  private List<Field> fields;

  @OneToMany(mappedBy = "partconstruct", cascade = CascadeType.ALL)
  private List<Hardparameter> hardparameters;

  @ManyToOne
  @JoinColumn(name = "construct_id")
  private Construct construct;

  //...
}

@Entity
public class Field extends ParentEntity {

  @Column
  private int fieldSize;

  @ManyToOne
  @JoinColumn(name = "partconstruct_id")
  private Partconstruct partconstruct;

  //...
}

@Entity
public class Hardparameter extends ParentEntity {

  @Column
  private String value;

  @ManyToOne
  @JoinColumn(name = "partConstruct_Id")
  private Partconstruct partConstruct;

  //...
}

我们关心Construct类型的对象。 Construct被深深地克隆和持久化,对象图上的所有嵌套对象也被克隆,并获得了新的Id(主键)。在每个克隆上,diffId(来自DiffEntity实体)都保持不变(其目的是使对象具有差异特征。)

鉴于以下内容,如何搜索和获取特定DiffEntity的参考:

  • Construnct实例的引用
  • 嵌套对象的类型
  • diffId我们正在追寻。

我尝试了具有反射功能的不同版本的对象图遍历器,这些对象对较小的Construct对象有效,但是一旦变得太大,性能就会非常慢。

实体管理器本身是否可以实现此目标?

0 个答案:

没有答案