We have noticed a critical issue with loading of entities with MapperSuperClasses having generic collections. The entities are not loaded correctly, where these generic collection fields are configured with other entities extending the same MapperSuperClass. Let me explain the situation with an example: Let’s say we have a MappedSuperClass A with a generic collection field:
@MappedSuperclass
public abstract class Configuration<T> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@OneToMany(mappedBy = "configuration", cascade = ALL, orphanRemoval = true)
private Set<T> items;
}
and subtypes:
The following jpql query throws an error:
select config from BConfiguration config
left join treat(config.items as BItem) item
left join item.children child
left join child.id
Stacktrace:
So from the error it seems hibernate thinks ‘children' field belongs to the 'com.example.demo.entities.AItem' which doesn’t have this field. Also I noticed that hibernate searches for a suitable type for the generic collection and it assigns it the first subtype it encounters such as AConfiguration instead of BConfiguration. If we had renamed Aconfiguration to CConfiguration then it doesn’t complain anymore. This is a blocker for us and prevents us from using generic collections in the MappedSuperClasses. |