-->
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.  [ 9 posts ] 
Author Message
 Post subject: Using dot notation in queries permitted in 3.2?
PostPosted: Sat Jul 14, 2007 10:26 am 
Beginner
Beginner

Joined: Sat Jul 08, 2006 2:58 pm
Posts: 26
Under 3.1.3 I had a dao method like the following:

Code:
public List getPersonByNameAndOrganization(final String firstname, final String lastname, final String employer) {
      HibernateTemplate ht = new HibernateTemplate(this.sessionFactory);
      return (List) ht.execute(new HibernateCallback() {
         public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
            
            Query query = session.createQuery("from Person p where p.firstName = :firstName and p.lastName = :lastName and p.occupation.employer.organizationName = :employer");
            query.setParameter("firstName", firstname);
            query.setParameter("lastName", lastname);
            query.setParameter("employer", employer);
            return query.list();
         }
      });
   }


This all worked fine until I upgraded to 3.2.4.sp1. Now the query returns a zero size list all the time even though I use the same dataset as I had when running 3.1.3.

Is this something isn't generally permitted and I have been lucky so far that it has worked?

Thanks in advance for educating me.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 4:43 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I just recreated, in minimalistic style, your situation and ran your query in 3.2.4sp1. I'm getting a row returned from my test case. Must be something other than the upgrade.

Mike


Employer.java
Code:
package test.jdcyuen;

public class Employer {
   private Long id;
   private String organizationName;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getOrganizationName() {
      return organizationName;
   }

   public void setOrganizationName(String organizationName) {
      this.organizationName = organizationName;
   }
   
   
}


Occupation.java
Code:
package test.jdcyuen;

public class Occupation {
   private Long id;
   private Employer employer;
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   public Employer getEmployer() {
      return employer;
   }
   public void setEmployer(Employer employer) {
      this.employer = employer;
   }
   
}


Person.java
Code:
package test.jdcyuen;

public class Person {
   private Long id;
   private String firstName;
   private String lastName;
   private Occupation occupation;
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public Occupation getOccupation() {
      return occupation;
   }
   public void setOccupation(Occupation occupation) {
      this.occupation = occupation;
   }
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }

   
}


Employer.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="test.jdcyuen">
   <class name="Employer" table="jdcyuen_employer">

      <id name="id" column="id">
         <generator class="increment" />
      </id>

   <property name="organizationName"/>
   
   </class>
</hibernate-mapping>


Occupation.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="test.jdcyuen">
   <class name="Occupation" table="jdcyuen_occupation">

      <id name="id" column="id">
         <generator class="increment" />
      </id>

     <many-to-one name="employer"/>
    
   </class>
</hibernate-mapping>


Person.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="test.jdcyuen">
   <class name="Person" table="jdcyuen_person">

      <id name="id" column="id">
         <generator class="increment" />
      </id>

     <property name="firstName"/>
     <property name="lastName"/>
     <many-to-one name="occupation"/>
    
   </class>
</hibernate-mapping>


TestIt
Code:
package test.jdcyuen;

import java.util.List;

import junit.framework.TestCase;

import org.hibernate.Query;
import org.hibernate.Session;

public class TestIt extends TestCase {

   public void testIt() {
      Long id = createPersonInDatabase();
      Session s = HibernateUtil.getSession();
      Employer e = (Employer)s.createCriteria(Employer.class).uniqueResult();
      
      Query query = s.createQuery("from Person p where p.firstName = :firstName and p.lastName = :lastName and p.occupation.employer.organizationName = :employer");
        query.setParameter("firstName", "fn");
        query.setParameter("lastName", "ln");
        query.setParameter("employer", "on");
        List results = query.list();
        System.out.println("size="+results.size());
        s.close();

   }

   private Long createPersonInDatabase() {
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      Employer e = new Employer();
      e.setOrganizationName("on");
      s.save(e);

      Occupation o = new Occupation();
      o.setEmployer(e);
      s.save(o);
      
      Person p = new Person();
      p.setFirstName("fn");
      p.setLastName("ln");
      p.setOccupation(o);
      Long id = (Long)s.save(p);
      
      s.getTransaction().commit();
      s.close();
      return id;
   }
}


Console
Code:
Hibernate: select max(id) from jdcyuen_employer
Hibernate: select max(id) from jdcyuen_occupation
Hibernate: select max(id) from jdcyuen_person
Hibernate: insert into jdcyuen_employer (organizationName, id) values (?, ?)
Hibernate: insert into jdcyuen_occupation (employer, id) values (?, ?)
Hibernate: insert into jdcyuen_person (firstName, lastName, occupation, id) values (?, ?, ?, ?)
Hibernate: select this_.id as id2_0_, this_.organizationName as organiza2_2_0_ from jdcyuen_employer this_
Hibernate: select person0_.id as id0_, person0_.firstName as firstName0_, person0_.lastName as lastName0_, person0_.occupation as occupation0_ from jdcyuen_person person0_, jdcyuen_occupation occupation1_, jdcyuen_employer employer2_ where person0_.occupation=occupation1_.id and occupation1_.employer=employer2_.id and person0_.firstName=? and person0_.lastName=? and employer2_.organizationName=?
size=1


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 2:32 am 
Beginner
Beginner

Joined: Sat Jul 08, 2006 2:58 pm
Posts: 26
This is very similar to my unit test which fails. Does anyone have any ideas as what I might check. I have no idea. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 2:50 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I'd check the generated SQL and perhaps compare with generated SQL from 3.1.3


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 4:45 pm 
Beginner
Beginner

Joined: Sat Jul 08, 2006 2:58 pm
Posts: 26
Isn't hibernate generating the sql? And if it's different then what does that tell me?

Thanks for your help, btw.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 7:38 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Yes, hibernate is generating the SQL but its often very useful to see what SQL it generates. Set the show_sql property to true to see what's going on.

If its different then there's been a change between 3.1.3 and the later version that's causing your queries to fail. If it isn't then there's something else wrong.

Inspecting the SQL queries is the best way to find out why you're not getting the result set you anticipated.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 3:16 pm 
Beginner
Beginner

Joined: Sat Jul 08, 2006 2:58 pm
Posts: 26
I've finally had a chance to followup on this and view the generated sql. It's different. But I don't know why?? Can anyone help me figure out an explanation for this.

On 3.1.3 the generated sql is:


Code:
[junit] 11:24:35,847 DEBUG SQL:346 -

select person0_.PersonID as PersonID38_,
person0_.cardID as cardID38_,
person0_.cardIssued as cardIssued38_,
person0_.lastModified as lastModi4_38_,
person0_.firstName as firstName38_,
person0_.middleName as middleName38_,
person0_.lastName as lastName38_,
person0_.suffix as suffix38_,
person0_.title as title38_,
person0_.alternateName as alterna10_38_,
person0_.phoneticSpelling as phoneti11_38_,
person0_.demographicID as demogra12_38_,
person0_.AlertInformationID as AlertIn13_38_,
person0_.occupationID as occupat14_38_,
person0_.socialhistoryID as socialh15_38_,
person0_.growthID as growthID38_ from person person0_,
occupation occupation1_,
organization organizati2_ where occupation1_.organizationID=organizati2_.OrganizationID and person0_.occupationID=occupation1_.occupationID and person0_.firstName=? and person0_.lastName=? and organizati2_.organizationName=?
    [junit] ------------- ---------------- ---------------

complete:



But on 3.2.4.sp1 the generated sql is this:

Code:
[junit] 11:47:23,903 DEBUG SQL:401 -
select person0_.PersonID as PersonID38_,
person0_.cardID as cardID38_,
person0_.cardIssued as cardIssued38_,
person0_.lastModified as lastModi4_38_,
person0_.firstName as firstName38_,
person0_.middleName as middleName38_,
person0_.lastName as lastName38_,
person0_.suffix as suffix38_,
person0_.title as title38_,
person0_.alternateName as alterna10_38_,
person0_.phoneticSpelling as phoneti11_38_,
person0_.demographicID as demogra12_38_,
person0_.AlertInformationID as AlertIn13_38_,
person0_.occupationID as occupat14_38_,
person0_.socialhistoryID as socialh15_38_,
person0_.growthID as growthID38_ from person person0_,
occupation occupation1_,
organization organizati2_ where person0_.occupationID=occupation1_.occupationID and
occupation1_.organizationID=organizati2_.OrganizationID and
person0_.firstName=? and person0_.lastName=? and organizati2_.organizationName=?

    [junit] 11:47:23,934 DEBUG SQL:401 - update person set cardID=?, cardIssued=?, lastModified=?, firstName=?, middleName=?, lastName=?, suffix=?, title=?, alternateName=?, phoneticSpelling=?, demographicID=?, AlertInformationID=?, occupationID=?, socialhistoryID=?, growthID=? where PersonID=?
    [junit] ------------- ---------------- ---------------
    [junit] Testcase: testGetPersonRecordsByNameAndOrganization(com.chisq.common.dao.PersonDAOTest):    FAILED
    [junit] expected:<1> but was:<0>
    [junit] junit.framework.AssertionFailedError: expected:<1> but was:<0>
    [junit]     at com.chisq.common.dao.PersonDAOTest.testGetPersonRecordsByNameAndOrganization(PersonDAOTest.java:91)



BUILD FAILED



Why would it be different? All I'm trying to do is to upgrade hibernate versions.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 3:09 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
The select query is effectively identical. The where clause ordering is slightly different but won't have any effect. What's interesting is the update that happens after the select. Are you changing the objects in any way? Are you running _exactly_ the same code and just changing hibernate versions between runs?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 4:49 pm 
Beginner
Beginner

Joined: Sat Jul 08, 2006 2:58 pm
Posts: 26
As a followup all I did was switch versions between runs.


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