Search code examples
hibernatecriteriahibernate-criteria

Criteria Query unable to fetch Data


I've a hbm file for my database view with all the columns as composite key

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="false">
    <class name="com.sample.dataobject.MyView"
        table="MY_VIEW" >
        <composite-id>
            <key-property name="code" type="string">
                <column name="CODE" length="2" />
            </key-property>
            <key-property name="Id" type="string">
                <column name="ID" length="14" />
            </key-property>
            <key-property name="Desc" type="string">
                <column name="DESC" length="256" />
            </key-property>
            <key-property name="TypeCode" type="string">
                <column name="TYPE_CODE" length="2" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

In the above view, the column DESC can be a null value.

I've written a criteria query to fetch results based on CODE and ID

Criteria c = getSession().createCriteria(MyView.class);
    c.add(Restrictions.eq("code", 'US'));
    c.add(Restrictions.eq("Id", '12'));
    List<MyView> annuityServices= c.list();

There are 2 rows in the View which satisfies these conditions, but the List which is returned is [null,null]

Also when i put a logger to check the query and value, it returns 2 rows but break at DESC column which is null

    2017-06-07 17:08:19 TRACE BasicExtractor:78 - extracted value ([CODE_1_390_0_] : [VARCHAR]) - [US]
2017-06-07 17:08:19 TRACE BasicExtractor:78 - extracted value ([ID_2_390_0_] : [VARCHAR]) - [12]
2017-06-07 17:08:19 TRACE BasicExtractor:68 - extracted value ([DESC_9_390_0_] : [VARCHAR]) - [null]
2017-06-07 17:08:19 TRACE BasicExtractor:78 - extracted value ([CODE_1_390_0_] : [VARCHAR]) - [US]
2017-06-07 17:08:19 TRACE BasicExtractor:78 - extracted value ([ID_2_390_0_] : [VARCHAR]) - [12]
2017-06-07 17:08:19 TRACE BasicExtractor:68 - extracted value ([DESC_9_390_0_] : [VARCHAR]) - [null]

Is there something wrong, or criteria API breaks when a null value is encountered?

Thanks


Solution

  • In the above view, the column DESC can be a null value.

    According to Hibernate documentation, primary key have to be not null:

    The values cannot be null. For composite ids, no part can be null.