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);
}
}
}