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: NoSuchElementException
PostPosted: Wed Aug 16, 2006 9:25 pm 
Beginner
Beginner

Joined: Sun Aug 13, 2006 8:48 am
Posts: 23
Hibernate version: 3.2

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


<hibernate-mapping>

   <class name="ergal.Category" table="CATEGORIES" lazy="true">
      <id name="id" type="long" column="ID">
         <generator class="increment" />
      </id>
   
      <property name="name" type="string">
         <column name="NAME" length="15" />
      </property>
      <set
         name="childCategories"
         cascade="save-update"
         inverse="true"
         >      
         <key column="CATEGORY_ID" />
         <one-to-many class="ergal.Category" />
      </set>
      
      <many-to-one
         name="parentCategory"
         column="CATEGORY_ID" 
         class="ergal.Category"
      />
   </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Code:
   public Category findCategoryByName(Session session, String name)throws Exception
   {
      List results = session.createQuery("from Category as c where c.name='" + name +"'").list();
      return (Category)results.iterator().next();
   }
   

Full stack trace of any exception that occurs:

[java] Exception in thread "main" java.util.NoSuchElementException
[java] at java.util.AbstractList$Itr.next(AbstractList.java:427)
[java] at ergal.BusinessService.findCategoryByName(Unknown Source)
[java] at ergal.BusinessService.modifyCategoryAssociation(Unknown Sourc
e)
[java] at ergal.BusinessService.test(Unknown Source)
[java] at ergal.BusinessService.main(Unknown Source)
[java] Java Result: 1


Name and version of the database you are using:mysql 5.0.22

Category.java
Code:
package ergal;
// Generated 2006-8-17 4:35:53 by Hibernate Tools 3.2.0.beta6a


import java.util.HashSet;
import java.util.Set;
import java.io.Serializable;

/**
* Category generated by hbm2java
*/
public class Category  implements java.io.Serializable {

    // Fields   

     private long id;
     private String name;
     private Set<Category> childCategories = new HashSet<Category>(0);
     private Category parentCategory;

     // Constructors

    /** default constructor */
    public Category() {}
   
    public Category(Set<Category> childCategories)
    {
       this.childCategories = childCategories;
    }

    /** full constructor */
    public Category(String name, Category parentCategory, Set<Category> childCategories)
    {
       this.name = name;
       this.childCategories = childCategories;
       this.parentCategory = parentCategory;
    }
   
   
    // Property accessors
    public long getId()
    {
        return this.id;
    }
   
    public void setId(long id)
    {
        this.id = id;
    }
    public String getName()
    {
        return this.name;
    }
   
    public void setName(String name)
    {
        this.name = name;
    }
    public Set<Category> getChildCategories()
    {
        return this.childCategories;
    }
   
    public void setChildCategories(Set<Category> childCategories)
    {
        this.childCategories = childCategories;
    }
    public Category getParentCategory()
    {
        return this.parentCategory;
    }
   
    public void setParentCategory(Category parentCategory)
    {
        this.parentCategory = parentCategory;
    }




}





BusinessService.java

Code:
package ergal;

import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class BusinessService
{
   public static SessionFactory sessionFactory;
   static
   {
      try
      {
         Configuration config = new Configuration();
         sessionFactory =config.configure().buildSessionFactory();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
   
   public void saveCategoryWithCaseCade()throws Exception
   {
      Session session = sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         
         Category foodCategory=new Category("food", null, new HashSet());
         Category fruitCategory=new Category("fruit", null, new HashSet());
         Category vegetableCategory=new Category("vegetable", null, new HashSet());
         Category appleCategory=new Category("apple", null, new HashSet());
         Category orangeCategory=new Category("orange", null, new HashSet());
         Category tomatoCategory=new Category("tomato", null, new HashSet());
         
         foodCategory.getChildCategories().add(fruitCategory);
         fruitCategory.setParentCategory(foodCategory);
         
         foodCategory.getChildCategories().add(vegetableCategory);
         vegetableCategory.setParentCategory(foodCategory);
         
         fruitCategory.getChildCategories().add(appleCategory);
         appleCategory.setParentCategory(fruitCategory);
         
         fruitCategory.getChildCategories().add(orangeCategory);
         orangeCategory.setParentCategory(fruitCategory);
         
         fruitCategory.getChildCategories().add(tomatoCategory);
         tomatoCategory.setParentCategory(fruitCategory);
         
         session.save(foodCategory);
         tx.commit();
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }
   
   public void modifyCategoryAssociation()throws Exception
   {
      Session session=sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         
         Category tomatoCategory=findCategoryByName(session,"tomato");
         Category fruitCategory=findCategoryByName(session, "fruit");
         Category vegetableCategory=findCategoryByName(session, "vagetable");
         
         vegetableCategory.getChildCategories().add(tomatoCategory);
         tomatoCategory.setParentCategory(vegetableCategory);
         
         fruitCategory.getChildCategories().remove(tomatoCategory);
         
         tx.commit();
         
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }
   
   public Category findCategoryByName(Session session, String name)throws Exception
   {
      List results = session.createQuery("from Category as c where c.name='" + name +"'").list();
      return (Category)results.iterator().next();
   }
   
   public void test()throws Exception
   {
      saveCategoryWithCaseCade();
      modifyCategoryAssociation();      
   }
   
   public static void main(String[] args)throws Exception
   {
      new BusinessService().test();
      sessionFactory.close();
   }
}



There is data in my database already after execute the saveCategoryWithCaseCade() method

How could the exception occur?


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.