I am newbie to Hibernate and Spring. My question is on “ how to add criteria…” in situation like, I have two beans:
1) Language:
public class Language {
private int languageId;
private String languageName;
private List<Topic> listOfAllTopics;
}
2) Topic:
public class Topic {
private int topicId;
private String topicName;
}
My database tables:
1) language
language_id int(PK)
language_name varchar(30)
2) topic
topic_id int(PK)
topic_name varchar(30)
language_id int(FK)
Hibernate mapping xml files are:
1) Language.hbm.xml
<hibernate-mapping package="com.mw.javamentordb.dto">
<class name="Language" table="language" lazy="true">
<id name="languageId" column="language_id" type="int">
<generator class="increment" />
</id>
<property name="languageName" column="language_name"type="string"/>
<bag name="listOfAllTopics" cascade="all">
<key column="language_id" not-null="true" />
<one-to-many class="Topic"/>
</bag>
</class>
</hibernate-mapping>
2) Topic.hbm.xml
<hibernate-mapping package="com.mw.javamentordb.dto">
<class name="Topic" table="topics" lazy="true">
<id name="topicId" column="topics_id" type="int">
<generator class="increment" />
</id>
<property name="topicName" column="topics_name" type="string" />
</class>
</hibernate-mapping>
And I get all lang in my database using this method and it works properly.
public List<Language> getAllLanguages() {
Criteria criteria = getSession().createCriteria(Language.class);
return criteria.list();
}
But when I try to get all topics of particular language(by langId) using criteria, it not works. Actually I don’t know how to use criteria in such kind of situation..
public List<Topic> getAllTopicOfLanguage(Language language) {
Criteria criteria = getSession().createCriteria(Topic.class);
criteria.add(Restrictions.eq("?");
return criteria.list();
}