-->
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.  [ 2 posts ] 
Author Message
 Post subject: Query Parameter
PostPosted: Wed Oct 20, 2010 7:51 pm 
Newbie

Joined: Wed Oct 20, 2010 7:19 pm
Posts: 1
Hi,

I have a table that has a nullable reference column to another table as seen in the following maps.

Code:
<hibernate-mapping>
  <class catalog="..." name="RecipientGroups" table="...">
    <id name="groupId" type="java.lang.Integer">
      <column name="group_id"/>
      <generator class="identity"/>
    </id>
    <property name="groupName" type="string">
      <column length="45" name="group_name" not-null="true"/>
    </property>
    <set inverse="true" name="recipientses">
      <key>
        <column name="group_id"/>
      </key>
      <one-to-many class="Recipients"/>
    </set>
  </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
  <class catalog="..." name=Recipients" table="...">
    <id name="recipientId" type="java.lang.Integer">
      <column name="recipient_id"/>
      <generator class="identity"/>
    </id>
    <many-to-one class="RecipientGroups" fetch="select" name="recipientGroups">
      <column name="group_id"/>
    </many-to-one>
    <property name="recipientName" type="string">
      <column length="45" name="recipient_name"/>
    </property>
    <property name="recipientNumber" type="string">
      <column length="45" name="recipient_number" not-null="true"/>
    </property>
  </class>
</hibernate-mapping>


group_id can be null in db design for ungrouped recipients, yet the mapping file as above.

When i run the following query within the program, i've got 0-size list but there is a record in db.
Code:
/* Some code here produces the parameters */
String name = "John Rainbow";
String number = "0033612546985";
RecipientGroups group = null;
/* Some code ...*/

List<Recipients> recipients = (List<Recipients>) session.createQuery(
                    "from Recipients " +
                    "where recipientName = :name " +
                    "and recipientNumber = :number " +
                    "and recipientGroups = :group")
                    .setParameter("name", name)
                    .setParameter("number", number)
                    .setParameter("group", group).list();


I ve tried the following hql on netbeans ide's HQL-Query window; it produces correct result.
Code:
from Recipients where recipientName = 'John Rainbow' and recipientNumber = '0033612546985' and recipientGroups = null;


For now, I'm not adding group parameter to the query and after checkin existence of the group in the result list in a for loop. Is something wrong on my side or is there a bug?


Top
 Profile  
 
 Post subject: Re: Query Parameter
PostPosted: Thu Oct 21, 2010 1:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
When comparing against null you usually have to use the 'is' operator. Eq.

Code:
where ... recipientGroups is null


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.