-->
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.  [ 3 posts ] 
Author Message
 Post subject: Loading collection using native SQL -- wrong results
PostPosted: Wed Feb 07, 2007 7:36 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
I'm trying to load a collection (set) through the mapping file using native SQL. The problem I'm encountering is two-fold: the set size is zero or one, and the entity loaded in the set is incorrect.

Here's a simple test case that shows the problem:

Tables:

Code:
drop table employee;
drop table department;
drop table company;

create table company (company_id numeric not null, name varchar2(32));
create table department (department_id numeric not null, company_id numeric not null, name varchar2(32));
create table employee (employee_id numeric not null, department_id numeric not null, name varchar2(32));

insert into company values (1, 'Company 1');
insert into company values (2, 'Company 2');
insert into company values (3, 'Company 3');

insert into department values (1, 1, 'Engineering');
insert into department values (2, 1, 'Sales');
insert into department values (3, 2, 'Marketing');
insert into department values (4, 2, 'Technical Support');
insert into department values (5, 3, 'Accounting');
insert into department values (6, 3, 'Human Resources');

insert into employee values (1, 1, 'Joe');
insert into employee values (2, 1, 'Bob');
insert into employee values (3, 2, 'Billy');
insert into employee values (4, 2, 'Mike');
insert into employee values (5, 2, 'Sam');
insert into employee values (6, 3, 'Pete');


Mapping file:

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>

  <class entity-name="Company" table="company">
    <id name="company_id" column="company_id" type="integer" />
    <property name="CompanyId" column="company_id" type="integer" update="false" insert="false"/>
    <property name="CompanyName" column="name" type="string" update="false" insert="false"/>
    <set name="employees">
      <key column="employee_id"/>
      <one-to-many entity-name="Employee"/>
      <loader query-ref="loadEmployees"/>
    </set>
  </class>

  <class entity-name="Employee" table="employee">
    <id name="employee_id" column="employee_id" type="integer" />
    <property name="EmployeeId" column="employee_id" type="integer" update="false" insert="false"/>
    <property name="EmployeeName" column="name" type="string" update="false" insert="false"/>
  </class>

  <sql-query name="loadEmployees">
    <load-collection alias="e" role="Company.employees"/>
    select {e.*} from department d, employee e where d.company_id = ? and e.department_id = d.department_id
  </sql-query>

</hibernate-mapping>


Java code:

Code:
public class Test
{
  public static void main(String[] args)
  {
    Configuration configuration = new Configuration();
    configuration.configure();
    SessionFactory factory = configuration.buildSessionFactory();
    Session session = factory.openSession();
    System.out.println("DEBUG> company1=" + session.get("Company", 1));
    System.out.println("DEBUG> company2=" + session.get("Company", 2));
    System.out.println("DEBUG> company3=" + session.get("Company", 3));
    System.out.println("DEBUG> employee1=" + session.get("Employee", 1));
  }
}


Screen output:

Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select company0_.company_id as company1_0_0_, company0_.name as name0_0_ from company company0_ where company0_.company_id=?
Hibernate: select e.employee_id as employee1_0__, e.employee_id as employee1_1_0_, e.name as name1_0_ from department d, employee e where d.company_id = ? and e.department_id = d.department_id
DEBUG> company1={employees=[{$type$=Employee, EmployeeName=Joe, EmployeeId=1, employee_id=1}], $type$=Company, CompanyId=1, company_id=1, CompanyName=Company 1}
Hibernate: select company0_.company_id as company1_0_0_, company0_.name as name0_0_ from company company0_ where company0_.company_id=?
DEBUG> company2={employees=[{$type$=Employee, EmployeeName=Bob, EmployeeId=2, employee_id=2}], $type$=Company, CompanyId=2, company_id=2, CompanyName=Company 2}
Hibernate: select company0_.company_id as company1_0_0_, company0_.name as name0_0_ from company company0_ where company0_.company_id=?
DEBUG> company3={employees=[{$type$=Employee, EmployeeName=Billy, EmployeeId=3, employee_id=3}], $type$=Company, CompanyId=3, company_id=3, CompanyName=Company 3}
DEBUG> employee1={$type$=Employee, EmployeeName=Joe, EmployeeId=1, employee_id=1}

Process finished with exit code 0


I was expecting to see 'Company 1' with five employees; Billy, Bob, Joe, Mike and Sam.

Any idea what's up and how to get the intended result?

Thanks,

-Kaare


Top
 Profile  
 
 Post subject: hi,i agree with you.
PostPosted: Thu Feb 08, 2007 2:30 am 
Newbie

Joined: Thu Feb 08, 2007 2:21 am
Posts: 2
i also add equal method.but still got wrong result.


Top
 Profile  
 
 Post subject: Re: hi,i agree with you.
PostPosted: Fri Feb 09, 2007 2:23 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
lxq2010 wrote:
i also add equal method.but still got wrong result.


The entities are map instances, not POJOs, so there's no need to add (override) equals() or hashCode().

Anyway, when I run the query manually like this:

Code:
    Query query = session.getNamedQuery("loadEmployees");
    query.setInteger("companyId", 1);
    for (Object employee : query.list()) {
      System.out.println("DEBUG> employee: " + employee);
    }


The output is:

Code:
DEBUG> employee: {$type$=Employee, EmployeeName=Joe, EmployeeId=1, id=1}
DEBUG> employee: {$type$=Employee, EmployeeName=Bob, EmployeeId=2, id=2}
DEBUG> employee: {$type$=Employee, EmployeeName=Billy, EmployeeId=3, id=3}
DEBUG> employee: {$type$=Employee, EmployeeName=Mike, EmployeeId=4, id=4}
DEBUG> employee: {$type$=Employee, EmployeeName=Sam, EmployeeId=5, id=5}


Any idea why only one element is being returned when executing the exact same query in the mapping file? The NamedQueryCollectionInitializer class returns a collection of size 5, but this seems to be thrown away somewhere along the way...

Could anyone who has gotten this to work before please help out? Any examples of working code would be greatly appreciated.

Thanks,

-Kaare


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