简体   繁体   中英

How to add Restrictions to Criteria API in Hibernate

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

Your table cheme for Topic has a foreign key constraint language_id but your class and hbm.xml mapping does not.

So your desired query is not possible.

Change it to:

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

And at the property to hbm.xml:

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

Then you can query it using criteria like following:

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

Alternatively you could use the equality on the object itself instead their id's or use Expression.eqId(Object Object)

Advice:

Use a abstract superclass with the identifier field to make things more generic. Naming the identifier once topicId on class and table Topic and languageId on class and table Language just is overhead. Just use id on property, class and table to make things easier. In larger applications this will become more obvious.

you are searching your result through wrong way, as per your design, you will get the result through,

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

this will return you Language DTO object, with list of topics matching the language id filled in Language DTO only.

as you are newbie, just want to tell you that when you are using hibernate, your DTO and table design should be very precise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM