我有一个声明为
的类@Entity
@Table(name = "word", schema = "public")
public class Word implements java.io.Serializable {
...
private ContentLanguage contentLanguage;
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "content_language_id", nullable = false)
public ContentLanguageT getContentLanguage() {
return this.contentLanguage;
}
public void setContentLanguage(ContentLanguage contentLanguage) {
this.contentLanguage = contentLanguage;
}
...
ContentLanguage声明为
@Entity
@Table(name = "content_language", schema = "public")
public class ContentLanguage implements java.io.Serializable {
private int id;
private String value;
....
我的操作需要查询ContentLanguage字段的id = 1的所有单词 我以为我可能能够针对ContentLanguage类创建一个条件,然后将其添加到针对Word表的Criteria中,但显然这不是这样做的方法。 任何想法,因为我不想继续使用SQL或HQL来完成相对简单的任务?
public String execute()
{
//DetachedCriteria criteria1 = DetachedCriteria.forClass(ContentLanguageT.class);
//criteria1.add(Restrictions.eq("id",1));
DetachedCriteria criteria = DetachedCriteria.forClass(WordT.class);
//criteria.add( criteria1 );
criteria.add(Restrictions.eq("contentLanguageT", Integer.valueOf( contentLangID )));
allWords = wordDao.findByCriteria( criteria, 0, 50);
return SUCCESS;
}
答案 0 :(得分:2)
Word.contentLanguage
的类型为ContentLanguage
。所以你无法将它与整数进行比较。您只能将其与ContentLanguage进行比较。所以,要么你做
criteria.add(Restrictions.eq("contentLanguage.id", contentLanguageId));
或者你做
criteria.add(Restrictions.eq("contentLanguage",
session.load(ContentLanguage.class, contentLanguageId)));
我真的更喜欢HQL这样简单的静态查询。以下是不是更具可读性和直观性?
select w from Word w
where w.contentLanguage.id = :contentLanguageId