In Hibernate HQL, how can I leave a connection, get related objects of a subclass when multiple subclasses have a property with the same name?

Summary

In Hibernate, I am trying to LEFT JOIN FETCH properties from subclasses of the main class, where several subclasses have properties with the same name. Hibernation, however, retrieves only the first related objects of the subclass, not the others.

Background

I model genomic features (genes, transcripts and exons) and genetic variants in a hibernate-based system. Genes, transcripts, and exons are all subclasses of GenomicFeature, and each of them can have zero to many GenomicFeatures. Genes, in turn, have transcripts from zero to many — like Exons — and transcripts have zero to many genes and exons. Each of these relationships happens lazily. Sometimes, however, I want to get a variant and all its genomic features, as well as all the genomic functions associated with the immediate genomic features. For example, I want to get a specific variant: Genes / Transcripts / Exons associated with the variant, all gene transcripts, all Genes & Exons transcripts and all Exons transcripts.

Problem

When I make a request for the above, it works, except that Transcripts for the Genes are not retrieved, only Exon Transcripts are retrieved. I assume that this is because the property - gene.transcripts and exon.transcripts - has the same name.

What i tried

Main request in PagingAndSortingRepository below

@Query("SELECT v FROM Variant v" + " LEFT JOIN FETCH v.variantGenomicFeatures AS vgf" + " LEFT JOIN FETCH vgf.genomicFeature AS gf LEFT JOIN FETCH gf.genes LEFT JOIN FETCH gf.exons LEFT JOIN FETCH gf.transcripts" + " WHERE" + " v.id = (:id)") public Variant findOneByIdAndGenomicFeaturesEagerly(@Param("id") Integer id); 

I tried combining genomicFeature twice, once for Genes and once for Transcripts & Exons, but this does not work.

I tried only selecting Genes ( WHERE TYPE(gf) = Gene ), but looking at the query that it generates, it still only joins Excon Transcripts and then returns Genes.

+7
java hibernate
source share
2 answers

I get it. It just works. I had a syntax error in my HQL.

When there are properties / columns in a discrimination subclass that are not part of the root class, all properties of all subsiminator subclassifiers are available in HQL using the root class in the FROM statement.

+2
source share

I have not tried myself, but maybe with an alias in the fetch clause can help Hibernate distinguish the field?

 LEFT JOIN FETCH gf.genes as g LEFT JOIN FETCH gf.exons as e LEFT JOIN FETCH gf.transcripts as t 
0
source share

All Articles