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: composition with hibernate
PostPosted: Wed May 28, 2008 6:11 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hello everyone,

I have two classes:

Category and CategoryType

Code:
public class Category implements Serializable, Comparable<Object> {   private int id = 0;
   private String title = null;
   private String description = null;
   private Category parentCategory;
   private CategoryType categoryType;
   private Set<Category> childCategories = new HashSet<Category>();
   private Date creationDate = new Date();

               ..................
}

public class CategoryType implements Serializable, Comparable<Object> {   private int id = 0;
   private String title = null;
   private String description = null;
   private Date creationDate = new Date();

               ...................
}


with the following mapping in the Category.hbm.xml to the CategoryType class:
Code:
<many-to-one name="categoryType"
            column="id_type"
            insert="false"
            update="false"
            cascade="none"
            class="CategoryType"
            not-null="true"
            outer-join="true"
            foreign-key="category_fk_id_type"/>


Then i just want to get all the categories of a particular category type:
Code:
public Collection<?> findCategoryByCategoryType(int categoryTypeId)
         throws InfrastructureException {

      Session session = HibernateUtil.getSession();
      Collection<?> categories;
      CategoryType categoryType = new CategoryType();
      categoryType.setId(categoryTypeId);
      try {
         Criteria crit = session.createCriteria(Category.class);
         categories = crit.add(Example.create(categoryType)).list();
      }  catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return categories;
   }


but im getting the following exception:
Code:
java.lang.ClassCastException: com.cms.model.category.CategoryType
   at org.hibernate.criterion.Example.getEntityMode(Example.java:256)
   at org.hibernate.criterion.Example.toSqlString(Example.java:186)



I don“t know what i am doing wrong, could someone please help me?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 7:07 am 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
you create a criteria for the category - class but add a example from the category-type class...

what you need are restrictions:
Code:
crit.add((Restrictions.conjunction()
                .add(Restrictions.eq("type.id", typeId))
                .add(Restrictions.somethin-else));


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 7:22 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Thanks a lot, it works great now :)


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.