aspose file tools*
The moose likes Object Relational Mapping and the fly likes java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate." Watch "java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate." New topic
Author

java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate.

umesh annegirikar
Greenhorn

Joined: May 09, 2011
Posts: 28
I am using Struts2 and hibernate3 for web application development.I have used the following libraries for the development
antlr-2.7.2.jar
asm-3.1.jar
asm-attrs.jar
cglib-2.2.jar
commons-collections-3.1.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
hibernate-3.2.7.ga.jar
hibernate3.jar
hibernate-commons-annotations-3.2.0.final.jar
hibernate-core-3.6.3.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.12.0.GA.jar
mysql-connector-java-5.0.8-bin.jar
ognl-3.0.6.jar
struts2-core-2.0.14.jar
xwork-2.0.7.jar

My hibernate.cfg.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/UserDetails</property>
<property name="hibernate.connection.user">root</property>
<property name="hibernate.connection.password">uha123</property>
<mapping resource="com/Struts2Login/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

User.hbm.xml is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.Struts2Login.entity.Users" table="userdetails" catalog="users">
<id name="userId" type="java.lang.Long">
<column name="Id" not-null="true" length="11"></column>
<generator class="native"/>
</id>
<property name="name">
<column name="UserName" not-null="true" length="20"></column>
</property>
<property name="password">
<column name="Password" not-null="true" length="20"></column>
</property>
<property name="email">
<column name="Email" not-null="true" length="100"></column>
</property>
<property name="rDate">
<column name="Dor" not-null="false"></column>
</property>
</class>
</hibernate-mapping>

HibernateUtil java class is
package com.Struts2Login.dao;
import java.net.URL;

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

public class HibernateUtil {

private static final SessionFactory sessionFactory=buildSessionfactory();
//private static String path="/hibernate.cfg.xml";

public static SessionFactory buildSessionfactory()
{
try {
//URL url= HibernateUtil.class.getResource(path);
Configuration config=(Configuration) new Configuration().configure().buildSessionFactory();
return (SessionFactory) config;
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("Initial SessionFactory creation failed \t" +e);
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}

}

public static SessionFactory getSessionfactory() {
return sessionFactory;
}


}

The stacktrace i obtainned is as given below

Initial SessionFactory creation failed java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate.Session
java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate.Session
at com.Struts2Login.dao.HibernateUtil.buildSessionfactory(HibernateUtil.java:18)
at com.Struts2Login.dao.HibernateUtil.<clinit>(HibernateUtil.java:10)
at com.Struts2Login.dao.User_DAO.find(User_DAO.java:17)
at com.Struts2Login.dao.LoginAction.execute(LoginAction.java:17)
Please tell me where i get wrong ,thanks
thanks in advance
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

umesh annegirikar wrote:Configuration config=(Configuration) new Configuration().configure().buildSessionFactory();
return (SessionFactory) config;


What you want to do in your buildSessionfactory method with above code?


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
umesh annegirikar
Greenhorn

Joined: May 09, 2011
Posts: 28
Actually i have resolved the problem

Thanks ,
Umesh Annegirikar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate.