Davithbul
Davithbul

Reputation: 21

Hibernate querying on a parent class creates ineffective union query

I am using spring boot and hibernate and I have following data structure.

@Entity
public class Template {

  @Id
  private Integer id;

  @OneToMany(mappedBy = "template", orphanRemoval = true, fetch = FetchType.EAGER,cascade = CascadeType.ALL)
  @BatchSize(size = 30)
  private Collection<TemplateContent> contents;
}

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TemplateContent {

  @Id
  private String contentType;

  @Id
  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(name = "template_id", nullable = false)
  private Template template;
}

@Entity
public class SomeContent extends TemplateContent {

   private String someValue;
}

@Entity
public class SomeOtherContent extends TemplateContent {

   private String someOtherValue;
}

@Repository
public interface TemplateRepository extends JpaRepository<Template, Integer> {
  Page<Template> findByIdIn(Collection<Integer> ids, Pageable pageable);
}

When I call findByIdIn method it generates the following query:

SELECT ...
FROM   (SELECT content_type, 
               template_id, 
               some_value, 
               NULL AS some_other_value
        FROM   someContent
        UNION 
    SELECT content_type, 
               template_id, 
               some_other_value, 
               NULL AS some_value
        FROM   someOtherContent) contents0_ 
WHERE  contents0_.template_id IN ( ?, ?, ? ) 

which is ineffective, because MySQL can't use indexes over derived tables. Is there a way to generate a more effective query.

//this would be the desired query
SELECT ...
FROM   (SELECT content_type, 
               template_id, 
               some_value, 
               NULL AS some_other_value
        FROM   someContent
        WHERE template_id IN ( ?, ?, ? )
        UNION 
    SELECT content_type, 
               template_id, 
               some_other_value, 
               NULL AS some_value
        FROM   someOtherContent
        WHERE template_id IN ( ?, ?, ? )) contents_0_ ....

I tried also to use different inheritance strategies, but it seems like all of them have a similar drawback.

Upvotes: 0

Views: 152

Answers (1)

ValerioMC
ValerioMC

Reputation: 3176

If you want to have best performance in polymorphic query the best approach is to use strategy = InheritanceType.SINGLE_TABLE, probably you will pay performance with empty columns, but it depends which aspect is more important for you, also because strategy = InheritanceType.JOINED has same problems of TABLE_PER_CLASS

Upvotes: 1

Related Questions