I searched on the web but cannot filter out the solution. I get pointed to Spring and Hibernate but I am only interested in the solution with OpenJPA. I have a table in which I store the taxonomy tree. The key is the TAXON_ID and it is linked with its parent through the PARENT_ID. I created the following DTO named TaxonDto (I left out the fields that are of no importance):

  @Column(name="PARENT_ID")
  private Long      parentId;
  @Column(name="RANG", length=3, nullable=false)
  private String    rang;
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="TAXON_ID", nullable=false, unique=true, updatable=false)
  private Long      taxonId;

  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, targetEntity=TaxonnaamDto.class, orphanRemoval=true)   @JoinColumn(name="TAXON_ID", nullable=false, updatable=false, insertable=true)
  @MapKey(name="taal")
  private Map<String, TaxonnaamDto> taxonnamen  = new HashMap<>();

  @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, targetEntity=TaxonDto.class, orphanRemoval=true)   @JoinColumn(name="PARENT_ID", referencedColumnName="TAXON_ID", updatable=false, insertable=false)
  private TaxonDto  parent;

My problem lies in the @OneToOne. I did a test with FetchType.EAGER but then the size of the returned data is 3x bigger as with LAZY since I get all parents below the taxon. What I want is that the parent is fetched when the rang is a specific value namely 'oso'. I can live with a solution where the parent is not recursivly filled, the parent of the parent in this DTO must be null.

Is this possible and if so how can I do this? I have seen discriminators, fetchgroup and subclass DTO but it leaves me clueless.

Reply via email to