I need polymorphic projections that map differently for each subtype. I found this thread and tried to copy it, but it does not work. The type variable is set correctly, but the projection-sub-classes are not used. I could not find any news on this topic, last posts on this are 5 years old.
This is my Baseclass Material
public class Material implements FileOwner {
protected Long id;
String name;
int grade;
boolean favorite = false;
}
Subclass LinkMaterial
public class LinkMaterial extends Material {
String url;
}
Now my projections:
@Projection(name = "material", types = Material.class)
public interface DTOMaterial {
@Value("#{target.getClass().getSimpleName()}")
String getType();
Long getId();
String getName();
int getGrade();
boolean getFavorite();
}
And the inheriting projection
@Projection(name = "material", types = LinkMaterial.class)
public interface DTOLinkMaterial extends DTOMaterial{
String getURL();
}
This is my repository method to get the DTOs:
@Query(value = "SELECT m from Material m JOIN FETCH m.submissionTasks stm WHERE stm.id.submissionTaskId=:id")
<T> List<T> getForSubmissionTask(Long id, Class<T> type);
I use the method like this:
public List<DTOMaterial> getMaterialDTOsForSubmissionTask(Long id) {
return materialRepository.getForSubmissionTask(id, DTOMaterial.class);
}
If I check if my DTOMaterial instanceof DTOLinkMaterial it is always false. However, Hibernate definetely understands that the Material itself is a LinkMaterial as you can see in the json I get on my client.
{name: "test LinkMaterial", id: 62, type: "LinkMaterial", grade: 8, favorite: false}
So type is correct. I tried to change the name of the projection, but this did also not succeed.
Hope, that anybody can help me, thanks in advance!