【发布时间】:2021-08-23 13:45:56
【问题描述】:
我正在构建一个本地化的应用程序。有 4 个实体使用这个可本地化的系统,我为此使用了 java.util.Maps,例如:
public class ProductDAO extends AbstractDAO {
private static final long serialVersionUID = 2036153197095739149L;
@ElementCollection
@MapKeyColumn(name = "lang")
@Column(name = "title")
@CollectionTable(name = "product_title", joinColumns = @JoinColumn(name = "product_id"), indexes = @Index(columnList = "lang", unique = true))
private Map<String, String> title;
@ElementCollection
@MapKeyColumn(name = "lang")
@Column(name = "description")
@CollectionTable(name = "product_description", joinColumns = @JoinColumn(name = "product_id"), indexes = @Index(columnList = "lang", unique = true))
private Map<String, String> description;
@CollectionTable(name = "topics")
@ElementCollection(fetch = FetchType.EAGER)
private Set<String> topics;
private float price;
}
如何在 spring 中生成一个查询(很确定我不能使用自动生成的,我必须使用@Query),它为我提供了具有特定本地化的实体?例如我有这个类的投影:
public class ProductProjection {
private String title;
private String description;
private Set<String> topics;
private float price;
}
我的目标是给定 id(在第一类的 AbstractDAO 扩展中)和语言,例如英语,我可以使查询工作:
@Query("some query i have no idea how to make here")
Optional<ProductProjection> findByIdAndLanguage(String id, String lang);
【问题讨论】:
标签: java mysql spring spring-data