Hello everyone, I have the following db structure:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
abstract class BaseClass(
@Id
val uuid: String = UUID.randomUUID().toString(),
val attr0: String
) : Model()
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
abstract class BaseAssociationClass(
@Id
val uuid: String = UUID.randomUUID().toString(),
val attr0: String
) : Model()
@Entity
class AssociationClassChild0(
val attr1: String,
attr0: String
) : BaseAssociationClass(attr0 = attr0)
@Entity
class AssociationClassChild1(
attr0: String,
val attr2: String
) : BaseAssociationClass(attr0 = attr0)
@Entity
class BaseClassChild0(
attr0: String,
@ManyToOne
@JoinColumn(name = "association_class_uuid")
val associationClassChild0: AssociationClassChild0
) : BaseClass(attr0 = attr0)
@Entity
class BaseClassChild1(
attr0: String,
@ManyToOne
@JoinColumn(name = "association_class_uuid")
val associationClassChild1: AssociationClassChild1
) : BaseClass(attr0 = attr0)
- a first base class with two children classes using single table inheritance
- a second base class with two more children classes single table inheritance
The two children of the first base class has a many to one relation with the other children of the second base class, the foreign key for both relations is the same. Then when I execute this query:
QBaseClass().findList()
the following sql is generated:
select t0.dtype, t0.uuid, t0.attr0, t1.dtype, t0.association_class_uuid, t2.dtype, t0.association_class_uuid from base_class t0 join base_association_class t1 on t1.uuid = t0.association_class_uuid join base_association_class t2 on t2.uuid = t0.association_class_uuid; --bind() --micros(23808)
Note that the query is joining two times with base_association_class using the same conditions.
I think this somehow expected because I'm declaring two different many-to-one relations, but this way I can query by the specific attributes of every child class of BaseAssociationClass like:
QBaseClassChild0().associationClassChild0.uuid.eq("bla") or
QBaseClassChild1().associationClassChild1.uuid.eq("bla")
The problem with this is that as the number of children with the foreign key relation increases the number of duplicated joins also increases degrading the performance of the query.
I tried creating an intermediary class between BaseClass and the children that contain the relation definition, but I still faced the same issue.
Do you guys know if there is some way to tell ebean to not generate those extra joins? Or do you recommend some workaround to avoid this use case?