-->
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: Strange Criteria behavior for onetomany with eager fetching
PostPosted: Wed Nov 16, 2005 6:02 am 
Newbie

Joined: Fri Sep 02, 2005 9:03 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: Hibernate 3.1 rc2

Mapping documents:
I just setted EAGER fetch type for classes from org.hibernate.test.annotations.onetomany
Code:
@Entity
public class City {
   private Integer id;
   private String name;
   private List<Street> streets;
   private List<Street> mainStreets;

   @Id(generate=GeneratorType.AUTO)
   public Integer getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   @OneToMany(mappedBy="city", fetch=FetchType.EAGER)
   @OrderBy("streetName, id")
   public List<Street> getStreets() {
      return streets;
   }

   public void setStreets(List<Street> streets) {
      this.streets = streets;
   }

   @OneToMany(fetch=FetchType.EAGER)
   @JoinColumn(name="mainstreetcity_id")
   @OrderBy
   public List<Street> getMainStreets() {
      return mainStreets;
   }

   public void setMainStreets(List<Street> streets) {
      this.mainStreets = streets;
   }

   public void addMainStreet(Street street) {
      if (mainStreets == null) mainStreets = new ArrayList<Street>();
      mainStreets.add(street);
   }

}

Code:
@Entity
public class Street {
   private Integer id;
   private String streetName;
   private City city;
   
    @Id(generate=GeneratorType.AUTO)
   public Integer getId() {
      return id;
   }

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

   public String getStreetName() {
      return streetName;
   }

   public void setStreetName(String streetName) {
      this.streetName = streetName;
   }

   @ManyToOne
   public City getCity() {
      return city;
   }

   public void setCity(City city) {
      this.city = city;
   }
}

Code between sessionFactory.openSession() and session.close():
Code:
         
public void testCriteria() throws Exception {
                Session s;
      Transaction tx;
      s = openSession();
      tx = s.beginTransaction();
      
      City paris = new City();
      paris.setName("Paris");
      s.persist(paris);
      Street rochechoir = new Street();
      rochechoir.setStreetName("Rochechoir");
      rochechoir.setCity(paris);
      Street chmpsElysees = new Street();
      chmpsElysees.setStreetName("Champs Elysees");
      chmpsElysees.setCity(paris);
      Street grandeArmee = new Street();
      grandeArmee.setStreetName("Grande Arm�e");
      grandeArmee.setCity(paris);
      s.persist(rochechoir);
      s.persist(chmpsElysees);
      s.persist(grandeArmee);
      paris.addMainStreet(chmpsElysees);
      paris.addMainStreet(grandeArmee);
      
      tx.commit();
      s.clear();

      tx = s.beginTransaction();
      List<City> citiesFromCriteria = (List<City>) s.createCriteria(City.class).list();
      assertEquals(1, citiesFromCriteria.size()); //fails junit.framework.AssertionFailedError: expected:<1> but was:<6>
      tx.commit();
      s.close();


Debug level Hibernate log excerpt:
13:50:30,060 DEBUG SQL:344 - select this_.id as id0_2_, this_.name as name0_2_, streets2_.city_id as city3_4_, streets2_.id as id4_, streets2_.id as id1_0_, streets2_.streetName as streetName1_0_, streets2_.city_id as city3_1_0_, mainstreet3_.mainstreetcity_id as mainstre4_5_, mainstreet3_.id as id5_, mainstreet3_.id as id1_1_, mainstreet3_.streetName as streetName1_1_, mainstreet3_.city_id as city3_1_1_ from City this_ left outer join Street streets2_ on this_.id=streets2_.city_id left outer join Street mainstreet3_ on this_.id=mainstreet3_.mainstreetcity_id order by streets2_.streetName asc, streets2_.id asc, mainstreet3_.id asc
13:50:30,073 DEBUG IntegerType:123 - returning '2' as column: id1_0_
13:50:30,074 DEBUG IntegerType:123 - returning '2' as column: id1_1_
13:50:30,075 DEBUG IntegerType:123 - returning '1' as column: id0_2_
13:50:30,077 DEBUG StringType:123 - returning 'Champs Elysees' as column: streetName1_0_
13:50:30,078 DEBUG IntegerType:123 - returning '1' as column: city3_1_0_
13:50:30,079 DEBUG StringType:123 - returning 'Paris' as column: name0_2_
13:50:30,082 DEBUG IntegerType:123 - returning '1' as column: city3_4_
13:50:30,086 DEBUG IntegerType:123 - returning '2' as column: id4_
13:50:30,089 DEBUG IntegerType:123 - returning '1' as column: mainstre4_5_
13:50:30,090 DEBUG IntegerType:123 - returning '2' as column: id5_
13:50:30,091 DEBUG IntegerType:123 - returning '2' as column: id1_0_
13:50:30,092 DEBUG IntegerType:123 - returning '3' as column: id1_1_
13:50:30,092 DEBUG IntegerType:123 - returning '1' as column: id0_2_
13:50:30,094 DEBUG StringType:123 - returning 'Grande Arm�e' as column: streetName1_1_
13:50:30,095 DEBUG IntegerType:123 - returning '1' as column: city3_1_1_
13:50:30,096 DEBUG IntegerType:123 - returning '1' as column: city3_4_
13:50:30,097 DEBUG IntegerType:123 - returning '2' as column: id4_
13:50:30,097 DEBUG IntegerType:123 - returning '1' as column: mainstre4_5_
13:50:30,099 DEBUG IntegerType:123 - returning '3' as column: id5_
13:50:30,099 DEBUG IntegerType:123 - returning '3' as column: id1_0_
13:50:30,100 DEBUG IntegerType:123 - returning '2' as column: id1_1_
13:50:30,101 DEBUG IntegerType:123 - returning '1' as column: id0_2_
13:50:30,102 DEBUG IntegerType:123 - returning '1' as column: city3_4_
13:50:30,104 DEBUG IntegerType:123 - returning '3' as column: id4_
13:50:30,105 DEBUG IntegerType:123 - returning '1' as column: mainstre4_5_
13:50:30,106 DEBUG IntegerType:123 - returning '2' as column: id5_
13:50:30,106 DEBUG IntegerType:123 - returning '3' as column: id1_0_
13:50:30,107 DEBUG IntegerType:123 - returning '3' as column: id1_1_
13:50:30,108 DEBUG IntegerType:123 - returning '1' as column: id0_2_
13:50:30,108 DEBUG IntegerType:123 - returning '1' as column: city3_4_
13:50:30,109 DEBUG IntegerType:123 - returning '3' as column: id4_
13:50:30,111 DEBUG IntegerType:123 - returning '1' as column: mainstre4_5_
13:50:30,112 DEBUG IntegerType:123 - returning '3' as column: id5_
13:50:30,113 DEBUG IntegerType:123 - returning '1' as column: id1_0_
13:50:30,121 DEBUG IntegerType:123 - returning '2' as column: id1_1_
13:50:30,122 DEBUG IntegerType:123 - returning '1' as column: id0_2_
13:50:30,122 DEBUG StringType:123 - returning 'Rochechoir' as column: streetName1_0_
13:50:30,123 DEBUG IntegerType:123 - returning '1' as column: city3_1_0_
13:50:30,124 DEBUG IntegerType:123 - returning '1' as column: city3_4_
13:50:30,125 DEBUG IntegerType:123 - returning '1' as column: id4_
13:50:30,127 DEBUG IntegerType:123 - returning '1' as column: mainstre4_5_
13:50:30,128 DEBUG IntegerType:123 - returning '2' as column: id5_
13:50:30,128 DEBUG IntegerType:123 - returning '1' as column: id1_0_
13:50:30,129 DEBUG IntegerType:123 - returning '3' as column: id1_1_
13:50:30,130 DEBUG IntegerType:123 - returning '1' as column: id0_2_
13:50:30,130 DEBUG IntegerType:123 - returning '1' as column: city3_4_
13:50:30,131 DEBUG IntegerType:123 - returning '1' as column: id4_
13:50:30,132 DEBUG IntegerType:123 - returning '1' as column: mainstre4_5_
13:50:30,134 DEBUG IntegerType:123 - returning '3' as column: id5_

It seems select produces a 6 rows result but why each row returned as a separate entity? There is only one City in fact.


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