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: question about inner join and inner join fetch
PostPosted: Mon Oct 27, 2008 2:05 am 
Newbie

Joined: Mon Jun 20, 2005 11:14 pm
Posts: 13
Code:
public class Main {
   
   public static void main(String[] args) {
      HibernateUtil.setup("create table Supplier ( id int, name VARCHAR);");
      HibernateUtil.setup("create table Product ( id int, name VARCHAR, description VARCHAR, price double,supplierId int);");
      
      prepareData();
      Session session = HibernateUtil.currentSession();
      
        String hql = "from Supplier s inner join fetch s.products as p";
        Query query = session.createQuery(hql);
        List results = query.list();
        displaySupplierList(results);
       
        String hql2 = "from Supplier s inner join s.products as p";
        Query query2 = session.createQuery(hql2);
        List results2 = query2.list();
        displaySupplierList(results2);
      
      
        //HibernateUtil.checkData("select * from Supplier");
        //HibernateUtil.checkData("select * from Product");

   }
    static public void displaySupplierList(List list) {
        Iterator iter = list.iterator();
        if (!iter.hasNext()) {
            System.out.println("No suppliers to display.");
            return;
        }       
        while (iter.hasNext()) {
            Supplier supplier = (Supplier) iter.next();
            String msg = supplier.getName();
            String msg2 = ((Product)supplier.getProducts().get(0)).getName();
            System.out.println(msg + " " + msg2);
        }
    }




   private static void prepareData(){
        Session session = HibernateUtil.currentSession();

        Supplier supplier1 = new Supplier();
        supplier1.setName("Supplier Name 1");
        session.save(supplier1);
       
        Supplier supplier2 = new Supplier();
        supplier2.setName("Supplier Name 2");
        session.save(supplier2);       
       
        Product product1 = new Product("Product 1","Name for Product 1", 2.0);
        product1.setSupplier(supplier1);
        supplier1.getProducts().add(product1);
        session.save(product1);
       
        Product product12 = new Product("Product 2","Name for Product 2", 22.0);
        product12.setSupplier(supplier1);
        supplier1.getProducts().add(product12);       
        session.save(product12);
       
        Product product2 = new Product("Product 3", "Name for Product 3", 30.0);
        product2.setSupplier(supplier2);
        supplier2.getProducts().add(product2);
        session.save(product2);
       
        session.flush();
        HibernateUtil.closeSession();
   }
}




the result in console is
...
[java] Hibernate: select supplier0_.id as id0_, products1_.id as id1_, supplier0_.name as name1_0_, products1_.name as name0_1_, products1_.description as descript3_0_1_, products1_.price as price0_1_, products1_.supplierId as supplierId0_1_, products1_.supplierId as supplierId__, products1_.id as id__ from Supplier supplier0_ inner join Product products1_ on supplier0_.id=products1_.supplierId
[java] Supplier Name 1 Product 1
[java] Supplier Name 1 Product 1
[java] Supplier Name 2 Product 3
[java] Hibernate: select supplier0_.id as id0_, products1_.id as id1_, supplier0_.name as name1_0_, products1_.name as name0_1_, products1_.description as descript3_0_1_, products1_.price as price0_1_, products1_.supplierId as supplierId0_1_ from Supplier supplier0_ inner join Product products1_ on supplier0_.id=products1_.supplierId
[java] Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to Supplier [java] at Main.displaySupplierList(Main.java:38)
[java] at Main.main(Main.java:24)
[java] Java Result: 1

if not using fetch, iter.next() cannot be cast to Supplier, why?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2008 3:08 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The first query (the one with fetch join) is the same as:

Code:
select s from Supplier s inner join fetch s.products


since fetch join doesn't return the associated object(s) in the query results (http://www.hibernate.org/hib_docs/v3/re ... joins.html)

The second query is more like:

Code:
select s, p from Supplier s inner join s.products as p


where s and p are returned as an Object[]. My recommendation is to always use select to specify what is going to be returned by the query.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2008 3:23 am 
Newbie

Joined: Mon Jun 20, 2005 11:14 pm
Posts: 13
"where s and p are returned as an Object[]. ........"
Oh, it is not mentioned in the document, right?


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.