-->
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.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException while building the SessionFactory
PostPosted: Wed Aug 30, 2006 1:34 pm 
Newbie

Joined: Wed Aug 30, 2006 1:19 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I started working on Hibernate recently and I tried the tutorials from Hibernate website. they worked.
I am getting a classcastexception when building the SessionFactory. The classes and mapping documents are generated using Hibernate reverse Engineering tool. I created a class with main and trying to access the database. I have 5 mapping documents generated by reverse engineering. I tried to find any info for this error on the web and could not find any. if I run the project without the mapping files I don't get any error, but when the mapping files are added i get the classcastexception.

Any help from Hibernate gurus are greatly appreciated. If necessary I will post the cfg.xml file and mapping files.
Thanks...

Hibernate version:
3.1
Mapping documents:

Code between sessionFactory.openSession() and session.close():
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
//Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Session session = sessionFactory.getCurrentSession();

if (session != null) {
session.beginTransaction();
List results = session.createQuery("from EdpmattributeType").list();
if (results.size() > 0) {
for (int i = 0; i < results.size(); i++) {
EdpmattributeType attrType = (EdpmattributeType) results.get(i);
System.out.println("******************OUTPUT************");
System.out.println("AttributeId " + attrType.getAttrTypeId() +
" Attribute name: " + attrType.getAttrTypeId());
}
}
session.getTransaction().commit();
//HibernateUtil.getSessionFactory().close();
sessionFactory.close();
}
Full stack trace of any exception that occurs:
Building SessionFactory failed.java.lang.ClassCastException
java.lang.ExceptionInInitializerError
at edpm.RepositoryTest.main(RepositoryTest.java:42)
Caused by: java.lang.ClassCastException
at org.hibernate.tuple.PropertyFactory.buildVersionProperty(PropertyFactory.java:83)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:157)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:216)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at edpm.RepositoryTest.main(RepositoryTest.java:18)
Exception in thread "main"
Name and version of the database you are using:
SQL Server 2000
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html
Code:
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

//import com.hibernate.src.EdpmattributeType;
//import com.hibernate.src.HibernateUtil;

public class RepositoryTest {
   private static final long serialVersionUID = 1L;
   private static SessionFactory sessionFactory = null;
   
   public static void main(String[] args) {
      try {
         sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
         //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         Session session = sessionFactory.getCurrentSession();

      if (session != null) {
        session.beginTransaction();
      List results = session.createQuery("from EdpmattributeType").list();
         if (results.size() > 0) {
               for (int i = 0; i < results.size(); i++) {
                      EdpmattributeType attrType = (EdpmattributeType) results.get(i);
                      System.out.println("******************OUTPUT************");
                      System.out.println("AttributeId " + attrType.getAttrTypeId() +
                                         " Attribute name: " + attrType.getAttrTypeId());
                  }
         }
         session.getTransaction().commit();
          //HibernateUtil.getSessionFactory().close();
         sessionFactory.close();
      }   
         
      }catch (Throwable ex) {
         //log.error("Building session factory failed.", ex);
          // Make sure you log the exception, as it might be swallowed
            System.err.println("Building SessionFactory failed." + ex);
            throw new ExceptionInInitializerError(ex);
       
         //throw new ExceptionInInitializerError(ex);
      }
      
   }
}

_________________
rkg


Top
 Profile  
 
 Post subject: ClassCastException while building the SessionFactory - fixed
PostPosted: Thu Aug 31, 2006 8:00 am 
Newbie

Joined: Wed Aug 30, 2006 1:19 pm
Posts: 2
This is fixed -
The problem is if the mapping file and class definitions were not matching.
I have a column called "Version" in my database and when I did the reverse engineering this version field was created <version/> tag instead of <property/> tag. this was causing the problem.

When dealing with mappings one extra pair of eyes are really necessary I believe.

I am posting this solution just in case if some one is looking for a solution and wasting days looking for it.
thanks...

_________________
rkg


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.