-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Find by null property
PostPosted: Thu Jul 08, 2010 3:54 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I have a MySql sql statement from a legacy php application that is the following:

SELECT * FROM document WHERE category_id = '$categoryId' OR (category_id IS NULL AND '$categoryId' = '0') ORDER BY list_order;

As one can see, this statement allows for the finding of documents that have no categories set to them.

I'm trying to have it behave in the same way in Hibernate.

And so, I came up to the following code.
Code:
<hibernate-mapping>
    <class name="com.thalasoft.learnintouch.core.domain.Document" table="document" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <many-to-one name="documentCategory" class="com.thalasoft.learnintouch.core.domain.DocumentCategory" cascade="all" fetch="select">
            <column name="category_id" />
        </many-to-one>
        <property name="reference" type="string">
            <column name="reference" length="50" not-null="false" />
        </property>
        <property name="description" type="string">
            <column name="description" not-null="false" />
        </property>
        <property name="file" type="string">
            <column name="file" length="50" not-null="true" />
        </property>
        <property name="hide" type="boolean">
            <column name="hide" not-null="false" />
        </property>
        <property name="listOrder" type="int">
            <column name="list_order" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


Code:
   public List<Document> findByCategoryOrderById(DocumentCategory documentCategory) {
      Criteria criteria = getSession().createCriteria(Document.class);
      criteria.add(Restrictions.eq("documentCategory", documentCategory));
      criteria.addOrder(Order.asc("id"));
      return criteria.list();
   }


But my test fails when trying to retrieve the one document that has no category given to it.

documentCategories = documentDao.findByCategory(null);
assertEquals(1, documentCategories.size());

testException = java.lang.AssertionError: expected:<1> but was:<0>]]

Code:
   @Test
   public void testFindByCategory() {
      DocumentCategory documentCategory = new DocumentCategory();
      documentCategory.setName("images");
      documentCategory.setListOrder(0);
      documentCategory = documentCategoryDao.makePersistent(documentCategory);
      document1 = documentDao.makePersistent(document1);
      document0 = documentDao.makePersistent(document0);
      document0.setDocumentCategory(documentCategory);
      List<Document> documentCategories = documentDao.findByCategory(documentCategory);
      assertEquals(1, documentCategories.size());
      documentCategories = documentDao.findByCategory(null);
      assertEquals(1, documentCategories.size());
   }


Any idea ?

Thanks !


Top
 Profile  
 
 Post subject: Re: Find by null property
PostPosted: Thu Jul 08, 2010 4:20 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
By the way, here is the mapping for the category
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.thalasoft.learnintouch.core.domain.DocumentCategory" table="document_category" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <property name="name" type="string">
            <column name="name" length="50" not-null="true" />
        </property>
        <property name="description" type="string">
            <column name="description" not-null="false" />
        </property>
        <property name="listOrder" type="int">
            <column name="list_order" not-null="true" />
        </property>
        <set name="documents" inverse="true">
            <key>
                <column name="category_id" />
            </key>
            <one-to-many class="com.thalasoft.learnintouch.core.domain.Document" />
        </set>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Find by null property
PostPosted: Fri Jul 09, 2010 2:04 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Here is the Hibernate sql statement and the error message:

select
this_.id as id67_0_,
this_.version as version67_0_,
this_.category_id as category3_67_0_,
this_.reference as reference67_0_,
this_.description as descript5_67_0_,
this_.file as file67_0_,
this_.hide as hide67_0_,
this_.list_order as list8_67_0_
from
document this_
where
this_.category_id=?
order by
this_.list_order asc
07:53:11,333 DEBUG IntegerType:126 - binding null to parameter: 1
07:53:11,336 INFO TransactionalTestExecutionListener:279 - Rolled back transaction after test execution for test context [[TestContext@1c81ac0 testClass = DocumentDaoTest, locations = array<String>['classpath:spring-hibernate.xml', 'classpath:spring-hibernate-dao.xml', 'classpath:spring-data-source.xml'], testInstance = com.thalasoft.learnintouch.core.dao.DocumentDaoTest@514f7f, testMethod = testFindByCategory@DocumentDaoTest, testException = java.lang.AssertionError: expected:<1> but was:<0>]]


Top
 Profile  
 
 Post subject: Re: Find by null property
PostPosted: Fri Jul 09, 2010 2:50 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I found the solution. It was simple in fact.

Criteria criteria = getSession().createCriteria(Document.class);
if (documentCategory != null) {
criteria.add(Restrictions.eq("documentCategory", documentCategory));
} else {
criteria.add(Restrictions.isNull("documentCategory"));
}
criteria.addOrder(Order.asc("listOrder"));
return criteria.list();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.