我正在尝试将getter用于criteriaQuery。
我有一个Translation类和一个Theme类。我想搜索一个带有转换值的主题,而不是翻译键。
这是我的代码:
@Entity
public class Translation implements Serializable{
private static final long serialVersionUID = -6216704909713037946L;
static final String DEFAULT_LOCALE = "EN";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NaturalId
private String code;
private String locale;
private String value;
public Translation() {}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static String geTranslatedValue(Map<String, Translation> translations) {
if(translations.containsKey(LocaleContextHolder.getLocale().getCountry()))
return translations.get(LocaleContextHolder.getLocale().getCountry()).getValue();
else if(translations.containsKey(Translation.DEFAULT_LOCALE))
return translations.get(Translation.DEFAULT_LOCALE).getValue();
return null;
}
}
@Entity
@Access(AccessType.PROPERTY)
public class Theme implements Serializable{
private Map<String, Translation> names;
private String name;
public Theme() {
this.names = new HashMap<>();
}
@JoinColumn(name = "code", referencedColumnName = "name")
@OneToMany(fetch = FetchType.EAGER)
@MapKey(name = "locale")
@JsonIgnore
public Map<String, Translation> getNames() {
return names;
}
public void setNames(Map<String, Translation> names) {
this.names = names;
}
@Column(name="name")
public String getName() {
return Translation.geTranslatedValue(this.names);
}
public void setName(String name) {
this.name = name;
}
}
在我的Dao中,我尝试按名称命名。
criteriaList.add(builder.like(themeRoot.get("name"), "%val%"));
但比较不使用名称,即使使用@Access(AccessType.PROPERTY)
,它也会使用翻译代码。
我怎么解决?
答案 0 :(得分:0)
您可以通过在下面添加依赖项
来使用jpa元模型<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
</dependency>
您现在可以使用生成的jpa元模型类,如下所示
builder.like(themRoot.get(Theme_.name), val);