-->
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.  [ 7 posts ] 
Author Message
 Post subject: Strange behaviour using compound path expression in where
PostPosted: Wed Apr 19, 2006 11:22 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
Hi,

I'm experiencing awkward behaviour using compound path expression in the where clause, when I'm placing a restriction on a one-to-one association.

I expected the following HQL query to work the same regardless of whether the database column is on table X or table Y, but this is not the case. As I have now found out, the query "from X as x where x.y is null" will only work as expected if the foreign key column is in table X (or in both--a double one-to-one).

Hibernate version: 3.0.5

Mapping documents:
Code:
    <class name="A">
        <id name="id">
            <generator class="native"/>
        </id>
        <many-to-one name="b"
                     class="B"
                     unique="true"
        />
    </class>
   
    <class name="B">
        <id name="id">
            <generator class="native"/>
        </id>
        <one-to-one name="a"
                    class="A"
                    property-ref="b"
        />
    </class>


Code between sessionFactory.openSession() and session.close():

testWhereAIsNull()
Code:
      // one A and one B
      s.persist(new A());
      s.persist(new B());
      
      Query query = s.createQuery("from B as b where b.a is null");
      
      assertEquals(1, query.list().size()); // fails: expected <1>, but was <0>!


testWhereBIsNull()
Code:
      // one A and one B
      s.persist(new A());
      s.persist(new B());
      
      Query query = s.createQuery("from A as a where a.b is null");
      
      assertEquals(1, query.list().size());


The generated SQL:

testWhereAIsNull()
Code:
select b0_.id as id from B b0_ where b0_.id is null


testWhereBIsNull()
Code:
select a0_.id as id, a0_.b as b0_ from A a0_ where a0_.b is null


The SQL that's generated for testWhereAIsNull() seems to be way off. If I use "is null" it always returns 0 results, and if I use "is not null" it returns every row in the table.

Is this the expected behaviour?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 1:12 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
From what I understand, one-to-one is exactly that: one object links to one and only one object. You're trying to implement a one-to-one-or-zero relationship. Change your one-to-one to many-to-one unique="true", as that's a one-to-one-or-zero relationship.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 6:46 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
tenwit wrote:
From what I understand, one-to-one is exactly that: one object links to one and only one object. You're trying to implement a one-to-one-or-zero relationship. Change your one-to-one to many-to-one unique="true", as that's a one-to-one-or-zero relationship.


Hmm, I'm pretty sure the semantics is at most one, just like the other associations. If I wished to set a lower bound as well, I would use <column not-null="true">.

Can someone confirm or deny, please?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 5:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It might be faster to just try the alternative mapping and see if it helps :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 6:05 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
tenwit wrote:
It might be faster to just try the alternative mapping and see if it helps :)


Yes it works if I map both sides with many-to-one unique="true" but this way I have 2 unidirectional one-to-one associations, instead of a true bidirectional association. In fact that's how it was mapped before, but I changed it because I'm afraid it could give rise to some data integrity problems.

Thanks for the help though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 2:07 am 
Newbie

Joined: Mon Aug 07, 2006 12:43 am
Posts: 1
I've encountered this behavior too, and it seems like a clear deviation from what should be expected. And using two many-to-ones really isn't ideal...

Does anyone have any more information? Related forums posts? JIRA tickets?


Top
 Profile  
 
 Post subject: is this satisfy u?
PostPosted: Thu Nov 09, 2006 2:56 am 
Beginner
Beginner

Joined: Tue Aug 22, 2006 3:06 am
Posts: 25
Code:
<class name="A">
        <id name="id">
            <generator class="native"/>
        </id>
        <many-to-one name="b"
                     class="B"
                     column="a_id"
        />
    </class>
   
    <class name="B">
        <id name="id">
            <generator class="native"/>
        </id>

<set name="a" cascade="delete" inverse="true">
            <key column="b_id"/>
            <one-to-many class="A"/>
        </set>

                  </class>


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