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: mysql double left join returns wrong result
PostPosted: Wed Jun 24, 2009 4:04 am 
Newbie

Joined: Fri Nov 21, 2008 10:43 am
Posts: 3
I have 2 tables:

audit:
Code:
FROM_NUMBER TO_NUMBER
123               321
222               111
333               123


address_book_contact:
Code:
PHONE  NAME
123    Bill


this query
Code:
SELECT a.FROM_NUMBER, a.TO_NUMBER, b.NAME, c.NAME FROM audit a
   LEFT JOIN address_book_contact b
      ON (a.FROM_NUMBER=b.PHONE)
   LEFT JOIN address_book_contact c
      ON (a.TO_NUMBER=c.PHONE)



returns
Code:
123 321 Bill null
222 111 null null
333 123 null Bill

in mysql query browser and in java app using pure jdbc,

and returns
Code:
123 321 Bill Bill
222 111 null null
333 123 null null

in java app using hibernate.
As you can see, b.name is substituted instead of c.name

Is this a bug?


Top
 Profile  
 
 Post subject: Re: mysql double left join returns wrong result
PostPosted: Wed Jun 24, 2009 4:06 am 
Newbie

Joined: Fri Nov 21, 2008 10:43 am
Posts: 3
this is test class:

Code:
package foo;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;


public class JoinTest {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/jointest.xml");
        JoinTest client = (JoinTest) ctx.getBean("JoinTest");
        client.test();
    }

    private void test() {
        String q = "SELECT a.*, b.CONTACT_ID, b.CONTACT_NAME, c.CONTACT_ID, c.CONTACT_NAME FROM audit a LEFT JOIN address_book_contact b" +
                " ON a.FROM_NUMBER=b.PHONE LEFT JOIN address_book_contact c ON a.TO_NUMBER=c.PHONE ";
        Session s = sessionFactory.openSession();
        Query query = s.createSQLQuery(q);
        List result = query.list();
        System.out.println(result);
    }
}


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.