我有一个旨在国际化的MVC应用程序。我有一个表格MY_TABLE
,其中包含一个字段word
,我想将其翻译成几种语言。我创建了以下模型:
CREATE TABLE MY_TABLE(
id int PRIMARY KEY,
-- other fields that don't need to be translated
);
CREATE TABLE LANGUAGE(
id VARCHAR(4) PRIMARY KEY -- eg en
);
CREATE TABLE MY_TABLE_WORD_TRANSLATION(
id int PRIMARY KEY,
language_id VARCHAR(4),
my_table_id int,
word_translation VARCHAR(100),
FOREIGN KEY(language_id) REFERENCES LANGUAGE(id),
FOREIGN KEY(my_table_id) REFERENCES MY_TABLE(id)
);
现在使用Hibernate创建了相应的类。
@Entity
@Table(name="LANGUAGE")
public class Language {
private String id;
public Language(){}
public Language(String id) {
super();
this.id = id;
}
@Id
@Column(nullable=false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
@Entity
@Table(name="MY_TABLE")
public class MyTable {
private int id;
public MyTable(){}
public MyTable(int id) {
super();
this.id = id;
}
@GeneratedValue
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@Entity
@Table(name="MY_TABLE_WORD_TRANSLATION")
public class MyTableWordTranslation {
private int id;
private String translation;
private Language language;
private MyTable myTable;
public MyTableWordTranslation() {}
public RegionNameTranslation(int id, String translation, Language language, MyTable myTable) {
super();
this.id = id;
this.translation = translation;
this.language = language;
this.myTable = myTable;
}
@GeneratedValue
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(nullable=false)
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
@ManyToOne
@JoinColumn(name="language_id")
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
@ManyToOne
@JoinColumn(name="my_table_id")
public MyTable getMyTable() {
return myTable;
}
public void setMyTable(MyTable myTable) {
this.myTable = myTable;
}
}
所以问题是如何将MyTableWordTranslation字段映射到MyTable中,以便仅获取我需要的翻译:
SELECT mytable FROM MyTable mytable
@OneToMany
过滤器与@Where
进行映射,但是如何在此过滤器中设置参数:@Where(clause="language_id = 'en'")
?该语言已从客户端的GET请求中指定。谢谢
答案 0 :(得分:0)
假设您的映射有效,我认为您可能希望使用MyTableWordTranslation实体编写查询,因为它拥有许多优势,它将获得您要查找的“ MyTable”对象。
它在DAO类中,在HQL查询中,但是那我应该写什么查询?现在我已经从MyTable mytable中选择了mytable
我尝试使用@OneToMany和@Where过滤器进行映射,但是如何在此过滤器中设置参数:@Where(clause =“ language_id =
'en'“)?语言是在
中的GET请求中指定的 客户。
在运行时未构建查询的情况下,可以使用@NamedQueries批注对实体进行批注。在注释内部,您可以提供具有“ name”和“ query”属性的@NamedQuery注释数组。通过name属性,您可以引用您为查询值提供的查询。在这个特定的示例中,它看起来像这样。
@NamedQueries(value = {@NamedQuery(name = "MyTableWordTranslation.getWordByLanguage", query = "SELECT mtwt.mytable FROM MyTableWordTranslation mtwt where mtwt.myTable.id=:wordId and mtwt.language.id = :languageId"),
...more queries,
.,
.})
@Entity
@Table(name="MY_TABLE_WORD_TRANSLATION")
public class MyTableWordTranslation {
现在,当您要使用此查询时,可以像这样通过DAO中的实体管理器来访问它。
Query query = entityManager.createNamedQuery("MyTableWordTranslation.getWordByLanguage");
query.setParameter("wordId", wordId);
query.setParameter("languageId", languageId);
MyTable mt = (MyTable) query.getSingleResult();
以下链接应有助于更好地解释NamedQueries:https://www.javatpoint.com/hibernate-named-query