hibernate not picking sessionFactory
Posted
by
Satya
on Stack Overflow
See other posts from Stack Overflow
or by Satya
Published on 2012-09-11T09:26:59Z
Indexed on
2012/09/11
9:38 UTC
Read the original article
Hit count: 271
My application-context.xml is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/myDB</value></property>
<property name="username"><value>myUser</value></property>
<property name="password"><value>myUser</value></property>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<property name="dataSource"><ref bean="myDataSource"/></property>
<list>
<value>com/x/model/config/hibernate/user.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties" >
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="userdao" class="com.x.y.z.UserDao">
<property name="sessionFactory"><ref bean="mySessionFactory"/></property>
</bean>
</beans>
user.hbm.xml is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cpt.model">
<class name="User" table="user">
<id name="userId" column="id">
<generator class="native"/>
</id>
<property name="firstname" column="firstName" />
<property name="lastName" column="lastName"/>
<property name="login" column="login"/>
<property name="pass" column="pass"/>
<property name="superemail" column="superEmail"/>
</class>
</hibernate-mapping>
and the UserDao is
package com.x.y.z;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.x.model.User;
@Component
public class UserDao {
private SessionFactory sessionFactory;
public void addUser(User user) {
Session session;
try {
try {
session = getSessionFactory().openSession();
// session = sessionFactory.openSession();
session.save(user);
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
System.out.println("printing in the catch");
e.printStackTrace();
}
}
public SessionFactory getSessionFactory() {
System.out.println("returning session factory ::: sessionFactory == null :: "+sessionFactory.openSession());
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("this is setting session factory" + sessionFactory.getClass());
System.out.println("setting session factory ::: sessionFactory == null :: "+sessionFactory==null);
this.sessionFactory = sessionFactory;
System.out.println("setting session factory ::: sessionFactory == null :: "+this.sessionFactory.openSession().getClass());
System.out.println(getSessionFactory().openSession().isOpen());
}
}
However, I keep getting
14:45:09,929 INFO [org.hibernate.impl.SessionFactoryImpl] building session fact
ory
14:45:09,933 WARN [net.sf.ehcache.config.Configurator] No configuration found.
Configuring ehcache from ehcache-failsafe.xml found in the classpath: vfs:/C:/jb
/server/default/deploy/C.war/WEB-INF/lib/ehcache-1.1.jar/ehcache-failsafe.xml
14:45:10,007 INFO [org.hibernate.impl.SessionFactoryObjectFactory] Not binding
factory to JNDI, no JNDI name configured
14:45:10,008 INFO [org.hibernate.impl.SessionFactoryImpl] Checking 0 named quer
ies
14:45:10,017 INFO [STDOUT] this is setting session factoryclass $Proxy178
14:45:10,017 INFO [STDOUT] false
14:45:10,019 INFO [STDOUT] setting session factory ::: sessionFactory == null :
: class org.hibernate.impl.SessionImpl
14:45:10,020 INFO [STDOUT] returning session factory ::: sessionFactory == null
:: org.hibernate.impl.SessionImpl(PersistentContext[entitiesByKey={}] ActionQue
ue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemova
ls=[] collectionUpdates=[]])
It is giving sessionFactory null . Any Idea where am I failing ? Thanks
© Stack Overflow or respective owner