实体图就像 Hibernate 提取配置文件一样适用,无论您在关联上有什么注释。如果没有,则可能是 Spring Data 甚至 Hibernate 中存在错误。如果您使用重现问题的测试用例创建新的JIRA 问题,这可能是最好的。
话虽如此,我认为这是Blaze-Persistence Entity Views的完美用例。
我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义模型之间轻松映射,例如 Spring Data Projections on steroids。这个想法是您以您喜欢的方式定义您的目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。
使用 Blaze-Persistence Entity-Views 的示例 DTO 模型可能如下所示:
@EntityView(User.class)
public interface UserDto {
@IdMapping
Long getId();
String getName();
Set<RoleDto> getRoles();
@EntityView(Role.class)
interface RoleDto {
@IdMapping
Long getId();
String getName();
}
// Other mappings
}
查询是将实体视图应用于查询的问题,最简单的就是通过 id 进行查询。
UserDto a = entityViewManager.find(entityManager, UserDto.class, id);
Spring Data 集成允许您几乎像 Spring Data Projections 一样使用它:https ://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features