I'm using the Entity View Module of Blaze Persistence for mapping the following Hibernate entities to DTOs.
@Entity
@Table(name = "configuration")
public class ConfigurationEntity {
@Id
private UUID institution;
@OneToMany(mappedBy = "configuration", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<DescriptionEntity> descriptions;
}
@Entity
@Table(name = "description")
public class DescriptionEntity {
@EmbeddedId
private Identifier identifier;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("identifier_institution")
@JoinColumn(name = "identifier_institution")
private InstitutionConfigurationEntity configuration;
}
@Embeddable
public class Identifier implements Serializable {
private UUID institution;
private String language;
}
Using an entity view where all child entities are stored as a list works without problems. However, instead I want to create a Map<String, DescriptionView> where the key refers to DescriptionEntity.Identifier.language and values are views of DescriptionEntity.
According to documentation (https://persistence.blazebit.com/documentation/1.5/entity-view/manual/en_US/#collection-type-re-mapping) the @MappingIndex
should be used for that. But even for the simplified case when using DescriptionEntity.name instead of the composite key, the library always throws java.lang.IllegalArgumentException: Attribute 'name' not found on type 'ConfigurationEntity'
.
@EntityView(ConfigurationEntity.class)
public interface InstitutionView {
@IdMapping(value = "institution")
UUID getIdentifier();
List<DescriptionView> getDescriptions(); // this works
@MappingIndex("name") // Attribute 'name' not found on type 'ConfigurationEntity'
Map<String, DescriptionView> getDescriptions(); // this doesn't
}
I'm having a hard time understanding why blaze would think the name
attribute would be in ConfigurationEntity
instead of DescriptionEntity
. This has led me to believe that the entity relations are not defined properly, but I have reviewed them over and over again and couldn't find anything (I'm using hibernate-orm.implicit-naming-strategy=component-path
by the way, meaning that the composite key columns are named identifier_institution
and identifier_language
).
I would be glad for any recommendations on how to solve this!
I believe this is a bug in the type validation code related to the fact that your one-to-many mapping uses mappedBy
. I think it tries to find the attribute for the mapping index in the type that the mappedBy
path refers to, which is ConfigurationEntity
, but the resulting query would still be correct.
I just ran a quick test and was able to reproduce your issue, and now also created a GitHub issue for this: https://github.com/Blazebit/blaze-persistence/issues/1695
You have two ways to workaround this until this bug is fixed:
ConfigurationEntity
e.g. @Formula("null") String name