I have looked at Discriminator is ignored during massindexing and Problem using Multitenancy Discriminator with Hibernate Search I don’t think these pertain although maybe are related, not sure?
I am having issues with polymorphic mass indexing with highly nested objects here is a simplified version:
@Entity
@DiscriminatorColumn(name = "baseEntityType")
@Inheritance(strategy = InheritanceType.JOINED)
public class BaseEntity implements Serializable {
@Id
@DocumentId
@GenericField(aggregable = Aggregable.YES, sortable = Sortable.YES, searchable = Searchable.YES)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
protected Long id;
}
@Entity
public class BaseVehicle extends BaseEntity {
private String bodyType;
}
@Entity
public class DooredVehicle extends BaseVehicle {
private String doorType;
}
@Entity
public class BedVehicle extends DooredVehicle {
private String bedType;
}
@Entity
@Indexed
public class Car extends DooredVehicle {
public String carHood;
}
@Entity
@Indexed
public class Truck extends BedVehicle {
private String truckRoof;
}
When doing a mass indexing on Car’s I get the following two queries:
Car Id Loading Query:
select
b1_0.id
from
BaseEntity b1_0
join BaseVehicle b1_1 on b1_0.id=b1_1.id
join DooredVehicle b1_2 on b1_0.id=b1_2.id
left join BedVehicle b1_3 on b1_0.id=b1_3.id
left join Car b1_4 on b1_0.id=b1_4.id
left join Truck b1_5 on b1_0.id=b1_5.id
where
b1_0.baseEntityType in (?)
Car Entity Loading Query:
select
b1_0.id,
b1_0.baseEntityType,
b1_1.bodyType,
b1_2.bedType,
b1_3.doorType,
b1_4.carHood,
b1_5.truckRoof
from
BaseEntity b1_0
left join BaseVehicle b1_1 on b1_0.id=b1_1.id
left join BedVehicle b1_2 on b1_0.id=b1_2.id
left join DooredVehicle b1_3 on b1_0.id=b1_3.id
left join Car b1_4 on b1_0.id=b1_4.id
left join Truck b1_5 on b1_0.id=b1_5.id
where b1_0.id = any (?)
The truck queries are very similar:
Truck Id Loading Query:
select
b1_0.id
from
BaseEntity b1_0
join BaseVehicle b1_1 on b1_0.id=b1_1.id
join DooredVehicle b1_2 on b1_0.id=b1_2.id
left join BedVehicle b1_3 on b1_0.id=b1_3.id
left join Car b1_4 on b1_0.id=b1_4.id
left join Truck b1_5 on b1_0.id=b1_5.id
where
b1_0.baseEntityType in (?)
Truck Entity Loading Query:
select
b1_0.id,
b1_0.baseEntityType,
b1_1.bodyType,
b1_2.bedType,
b1_3.doorType,
b1_4.carHood,
b1_5.truckRoof
from
BaseEntity b1_0
left join BaseVehicle b1_1 on b1_0.id=b1_1.id
left join BedVehicle b1_2 on b1_0.id=b1_2.id
left join DooredVehicle b1_3 on b1_0.id=b1_3.id
left join Car b1_4 on b1_0.id=b1_4.id
left join Truck b1_5 on b1_0.id=b1_5.id
where
b1_0.id = any (?)
Where as when doing a regular hibernate query for a Car or Truck I get the following two queries:
entityManager.find(Car.class, id);
select
c1_0.id,
c1_1.bodyType,
c1_2.doorType,
c1_0.carHood
from
Car c1_0
join BaseVehicle c1_1 on c1_0.id=c1_1.id
join DooredVehicle c1_2 on c1_0.id=c1_2.id
where
c1_0.id=?
entityManager.find(Truck.class, id);
select
t1_0.id,
t1_1.bodyType,
t1_2.doorType,
t1_3.bedType,
t1_0.truckRoof
from
Truck t1_0
join BaseVehicle t1_1 on t1_0.id=t1_1.id
join DooredVehicle t1_2 on t1_0.id=t1_2.id
join BedVehicle t1_3 on t1_0.id=t1_3.id
where
t1_0.id=?
Car hibernate query does not include the BedVehicle table, nor the Truck table, and the issue is the mass indexing query does include those tables. As in I would expect the Car query for mass indexing to not include the BedVehicle nor the Truck tables as part of the query, for loading entities.
This is not a problem for small datasets but when you have hundreds of objects and they all extend the “BaseEntity” class, then the mass indexing query, basically joins ALL tables in the database together. Here is a link to the sample project running almost the latest quarkus.
Olin Blodgett / Inheritance · GitLab branch hibernate_search