-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with left outer join , multiple selects
PostPosted: Tue Oct 19, 2010 11:29 am 
Newbie

Joined: Tue Oct 19, 2010 10:17 am
Posts: 1
Hello, I am starting with Hibernate and after reading many tutorials and making many samples, I have my first problem I cant solve.

I am trying to make a left join, of two classes. Usuario, Pedido.

The Pedido has an usuario_id. The relation is many to one.

One usuario many Pedidos
Many pedidos one usuario.

The problem is that when I am trying to get the Usuario's Name , an N +1 select problem appears.
This is my code:
Code:
   <class name="com.expanding.domain.Pedido" table="pedido" >

      <id name="id" type="java.lang.Integer" >
         <column name="id"  />
         <generator class="native" />
      </id>
      <property name="prioridad" column="prioridad" type="java.lang.String" />
      <property name="operador" column="operador" type="java.lang.String" />

<many-to-one class="com.expanding.domain.Usuario"
         name="usuariopedido" not-found="ignore" column="usuario_ID" fetch="join"/>
      

   </class>


Code:
package com.expanding.domain;



public class Pedido {

   java.lang.Integer id;
   java.lang.String prioridad;
   java.lang.String operador;
   com.expanding.domain.Usuario usuariopedido;

   public Pedido() {
   }

   public Pedido(java.lang.Integer iD, java.lang.String prioridad,
         java.lang.String operador,
         com.expanding.domain.Usuario usuariopedido) {

      id = iD;
      this.prioridad = prioridad;
      this.operador = operador;
      this.usuariopedido = usuariopedido;

   }

   public Pedido(java.lang.Integer ID) {
      this.id = ID;

   }

   public java.lang.String getPrioridad() {
      return prioridad;
   }

   public void setPrioridad(java.lang.String prioridad) {
      this.prioridad = prioridad;
   }

   public java.lang.String getOperador() {
      return operador;
   }

   public void setOperador(java.lang.String operador) {
      this.operador = operador;
   }

   public java.lang.Integer getId() {
      return id;
   }

   public void setId(java.lang.Integer iD) {
      id = iD;
   }

   public com.expanding.domain.Usuario getUsuariopedido() {
      return usuariopedido;
   }

   public void setUsuariopedido(com.expanding.domain.Usuario usuariopedido) {
      this.usuariopedido = usuariopedido;
   }

}


I am trying to make the select in this way
Code:

      Criteria criteria = session.createCriteria(Pedido.class).setFetchMode(
            "usuariopedido", FetchMode.JOIN);

      ScrollableResults res = criteria.scroll(ScrollMode.FORWARD_ONLY);

      int rowStart = 0;
      res.last();
      int lastRow = res.getRowNumber();

      res.first();
      res.scroll(rowStart);
      System.out.println(lastRow);
      int i = 0;
      while (lastRow > i++) {
         items.add(res.get(0));

         System.out.println(i);
         session.flush();
         session.clear();

         if (!res.next())
            break;
      }


I also tried with hql
Code:
query = session.createQuery( "select pe from Pedido pe left outer join fetch pe.usuariopedido usuario" ); //
               
        List<Pedido> pedido = new ArrayList<Pedido>(query.list());
       
        if (pedido != null) { System.out.println(pedido.size());
       
      for (Pedido pedido1 : pedido) {
   items.add(pedido1);
    } }
   




This is the log the last part of the log
Quote:
44576
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44577
44578
44579
44580
44581
44582
44583
44584
44585
44586
44587
44588
44589
44590
44591
44592
44593
44594
44595
44596
44597
44598
44599
44600
44601
44602
44603
44604
44605
44606
44607
44608
44609
44610
44611
44612
44613
44614
44615
44616
44617
44618
44619
44620
44621
44622
44623
44624
44625
44626
44627
44628
44629
44630
44631
44632
44633
44634
44635
44636
44637
44638
44639
44640
44641
44642
44643
44644
44645
44646
44647
44648
44649
44650
44651
44652
44653
44654
44655
44656
44657
44658
44659
44660
44661
44662
44663
44664
44665
44666
44667
44668
44669
44670
44671
44672
44673
44674
44675
44676
44677
44678
44679
44680
44681
44682
44683
44684
44685
44686
44687
44688
44689
44690
44691
44692
44693
44694
44695
44696
44697
44698
44699
44700
44701
44702
44703
44704
44705
44706
44707
44708
44709
44710
44711
44712
44713
44714
44715
44716
44717
44718
44719
44720
44721
44722
44723
44724
44725
44726
44727
44728
44729
44730
44731
44732
44733
44734
44735
44736
44737
44738
44739
44740
44741
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44742
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44743
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44744
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44745
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44746
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?
44747
Hibernate: select usuario0_.ID as ID0_0_, usuario0_.apellido as apellido0_0_, usuario0_.nombre as nombre0_0_ from usuario usuario0_ where usuario0_.ID=?


There are some pedidos without usuarios

I want to solve this bug and avoid the multiple select, thanks

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.