1

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();
}
4

2 回答 2

1

您的 Topic 表方案具有外键约束language_id,但您的类和 hbm.xml 映射没有。

所以你想要的查询是不可能的。

将其更改为:

public class Topic 
{
    private int topicId;
    private String topicName;
    private Language language;
}

并在 hbm.xml 的属性处:

<many-to-one name="language" class="package.Language" fetch="select">
    <column name="language_id">
</many-to-one>

然后您可以使用如下条件查询它:

criteria.add(Expression.eq("language.language_id", language.getLanguageId()));

或者,您可以使用对象本身的相等性而不是它们的 id 或使用Expression.eqId(Object Object)

建议:

使用带有标识符字段的抽象超类使事情更通用。topicId在类和表Topic以及languageId类和表上命名标识符一次Language只是开销。只需id在属性、类和表上使用即可让事情变得更容易。在更大的应用程序中,这将变得更加明显。

于 2012-12-10T13:08:49.490 回答
0

您正在通过错误的方式搜索结果,根据您的设计,您将获得结果,

public Language getAllTopicOfLanguage(Language language) {
    Criteria criteria = getSession().createCriteria(Language.class);
    criteria.add(Expression.eq("languageId",language.getLanguageId()));
    return (Language)(criteria.list().get(0));
}

这将返回您的 Language DTO 对象,其中包含与语言 id 匹配的主题列表,仅填写 Language DTO。

作为新手,只想告诉你,当你使用hibernate时,你的DTO和表格设计应该是非常精确的。

于 2012-12-10T17:09:03.473 回答