Results 1 to 9 of 9

Thread: No SessionFactory specified

Hybrid View

  1. #1

    Default No SessionFactory specified

    I have an application where in Iam using Tapestry - Hibernate - Spring.
    Following are my Class files

    BaseDAO.java
    Code:
    package jeevan.spring.dao;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.springframework.orm.hibernate3.SessionFactoryUtils;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    public class BaseDAO extends HibernateDaoSupport
    {
    	public Session openSession()
    	{
    		return SessionFactoryUtils.getSession(getSessionFactory(),false);
    	}
    
    	public Query getQuery(String query) throws HibernateException
    	{
    		Session session = this.openSession();
    		return session.createQuery(query);
    	}
    }
    FnuDAOImpl
    Code:
    package jeevan.spring.dao.impl;
    
    import java.util.List;
    import java.util.Vector;
    
    import org.springframework.orm.hibernate3.HibernateTemplate;
    
    import jeevan.spring.*;
    import jeevan.spring.dao.BaseDAO;
    import jeevan.spring.dao.IFnuDao;
    
    public class FnuDAOImpl extends BaseDAO implements IFnuDao
    {
    	HibernateTemplate hibTemplate;
    	
    	public FnuDAOImpl()
    	{
    		hibTemplate = new HibernateTemplate();
    	}
    	
    	public Vector getStatus()
    	{
    		Vector vStatus = new Vector(4, 4);
    
    		CacheObject cObj = null;
    		Status obj = null;
    		System.out.println("Result Value");
    		List result = hibTemplate.loadAll(Status.class);
    			//this.getQuery("from Status ").list();
    		
    		System.out.println("Result Value"+result.size());
    		for(int i=0; i < result.size();i++){
    			obj = (Status)result.get(i);
    			cObj = new CacheObject();
    			cObj.setCacheId(obj.getId());
    			cObj.setCacheCode(obj.getCode());
    			vStatus.addElement(cObj);	
    		} 
    		
    		return vStatus;
    	}
    }
    Status.hbm.xml
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    	<class name="jeevan.spring.Status" table="FNU_STATUS" dynamic-insert="false" dynamic-update="false">
    	        
    		<id name="id" column="STATUS_ID" type="long">
    			<generator class="assigned"/>
    		</id>
    		<property name="code" column="STATUS_CODE"/>
    	</class>
    </hibernate-mapping>
    applicationContext.xml
    Code:
    <?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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="hibernate.connection.driver_class">
    			<value>oracle.jdbc.driver.OracleDriver</value>
    		</property>
    		<property name="hibernate.connection.url">
    			<value>jdbc:oracle:thin:@192.168.1.18:1521:regac</value>
    		</property>
    		<property name="hibernate.connection.username">
    			<value>fnur</value>
    		</property>
    		<property name="hibernate.connection.password">
    			<value>fnur</value>
    		</property>
    		<property name="current_session_context_class">
    			<value>thread</value>
    		</property>
    		<property name="cache.provider_class">
    			<value>org.hibernate.cache.NoCacheProvider</value>
    		</property>
    		<property name="show_sql">
    			<value>true</value>
    		</property>
    		<property name="hibernate.dialect">
    			<value>org.hibernate.dialect.Oracle9Dialect</value>
    		</property>
    		<property name="mappingResources">
    			<list>
    				<value>jeevan/spring/Status.hbm.xml</value>
    			</list>
    		</property>
    	</bean>
    	
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    	
    	<bean id="userDAO" class="jeevan.spring.dao.impl.FnuDAOImpl">
    		<property name="hibernateTemplate">
    			<ref local="hibernateTemplate" />
    		</property>
    	</bean>
    	
    </beans>
    Status.java
    Code:
    package jeevan.spring;
    
    import java.io.Serializable;
    
    public class Status implements Serializable
    {
    	private static final long serialVersionUID = 7526472295622776147L;
    	private long _id;
    	private String _code;
    
    	public Status()
    	{
    		_id = Integer.MIN_VALUE;
    		_code = ""; 
    	}
    
    	public long getId()
    	{
    		return _id;
    	}
    
    	public void setId(long Value)
    	{
    		_id = Value;
    	}
    
    	public String getCode()
    	{
    		return _code;
    	}
    
    	public void setCode(String Value)
    	{
    		_code = Value;
    	}
    	
    	public void print()
    	{
    		System.out.println("StatusId = " + _id);
    		System.out.println("StatusCode = " + _code);
    	}
    
    }
    IFnuDAO.java
    Code:
    package jeevan.spring.dao;
    
    import java.util.Vector;
    
    public interface IFnuDao
    {
    	public Vector getStatus();
    }
    TestJava.java
    Code:
    package jeevan;
    import java.util.Vector;
    
    import jeevan.spring.dao.IFnuDao;
    import jeevan.spring.dao.impl.FnuDAOImpl;
    
    public class TestJava
    {
    	Vector vStatus;
    	
    	public static void main(String srgs[])
    	{
    		IFnuDao fnuDao = new FnuDAOImpl();
    		
    		System.out.println(fnuDao.getStatus().size());
    	}
    }
    While running TestJava.java file I am getting the following error message:
    Code:
    Result Value
    java.lang.IllegalArgumentException: No SessionFactory specified
    	at org.springframework.util.Assert.notNull(Assert.java:90)
    	at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:293)
    	at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:194)
    	at jeevan.spring.dao.BaseDAO.openSession(BaseDAO.java:13)
    	at jeevan.spring.dao.BaseDAO.getQuery(BaseDAO.java:18)
    	at jeevan.spring.dao.impl.FnuDAOImpl.getStatus(FnuDAOImpl.java:28)
    	at jeevan.TestJava.main(TestJava.java:15)
    Exception in thread "main"

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    In your test class you are just instantiating a FnuDAOImpl, it doesn't get injected with the sessionfactory and hibernate template. You need to create a ApplicationContext and retrieve a instance from that.

    I strongly suggest you read the chapter about testing with the Spring Framework in the referenceguide.

    Also I would change your class to something like this:

    Code:
    package jeevan.spring.dao.impl;
    
    import java.util.List;
    import java.util.Vector;
    
    import org.springframework.orm.hibernate3.HibernateTemplate;
    
    import jeevan.spring.*;
    import jeevan.spring.dao.BaseDAO;
    import jeevan.spring.dao.IFnuDao;
    
    public class FnuDAOImpl extends BaseDAO implements IFnuDao
    {
    	public FnuDAOImpl() {}
    	
    	public Vector getStatus()
    	{
    		Vector vStatus = new Vector(4, 4);
    
    		CacheObject cObj = null;
    		Status obj = null;
    		System.out.println("Result Value");
    		List result = getHibernateTemplate().loadAll(Status.class);
    			//this.getQuery("from Status ").list();
    		
    		System.out.println("Result Value"+result.size());
    		for(int i=0; i < result.size();i++){
    			obj = (Status)result.get(i);
    			cObj = new CacheObject();
    			cObj.setCacheId(obj.getId());
    			cObj.setCacheCode(obj.getCode());
    			vStatus.addElement(cObj);	
    		} 
    		
    		return vStatus;
    	}
    }
    Your baseclass extends HibernateDaoSupport which already has a HibernateTemplate for you. No need to create a new one.

    Also why invoke SessionFactoryUtils yourself. The HibernateDaoSupport class has a method which does that for you. I would suggest to call getSession(false) instead of the SessionFactoryUtils method.
    Last edited by Marten Deinum; Dec 28th, 2006 at 04:38 AM. Reason: Added link to documentation
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default Re:

    Thanks for your reply.
    Can you tell me how to create a ApplicationContext and retrieve a instance from that?
    I have a file called applicationContext.xml
    Code:
    <?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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="hibernate.connection.driver_class">
    			<value>oracle.jdbc.driver.OracleDriver</value>
    		</property>
    		<property name="hibernate.connection.url">
    			<value>jdbc:oracle:thin:@192.168.1.18:1521:regac</value>
    		</property>
    		<property name="hibernate.connection.username">
    			<value>fnur</value>
    		</property>
    		<property name="hibernate.connection.password">
    			<value>fnur</value>
    		</property>
    		<property name="current_session_context_class">
    			<value>thread</value>
    		</property>
    		<property name="cache.provider_class">
    			<value>org.hibernate.cache.NoCacheProvider</value>
    		</property>
    		<property name="show_sql">
    			<value>true</value>
    		</property>
    		<property name="hibernate.dialect">
    			<value>org.hibernate.dialect.Oracle9Dialect</value>
    		</property>
    		<property name="mappingResources">
    			<list>
    				<value>jeevan/spring/Status.hbm.xml</value>
    			</list>
    		</property>
    	</bean>
    	
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    	
    	<bean id="userDAO" class="jeevan.spring.dao.impl.FnuDAOImpl">
    		<property name="hibernateTemplate">
    			<ref local="hibernateTemplate" />
    		</property>
    	</bean>
    	
    </beans>
    Also can you provide me with the links for "testing with the Spring Framework in the referenceguid"

    Liju

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    The link was already there, just click it .

    Well the easiest way would be to extend the Spring TestCase and let that work its magic for you. However you are not using JUnit for as far as I can see so someting like the following would do.

    Code:
    package jeevan;
    import java.util.Vector;
    
    import jeevan.spring.dao.IFnuDao;
    import jeevan.spring.dao.impl.FnuDAOImpl;
    
    public class TestJava {
    
    
    	Vector vStatus;
    	
    	public static void main(String srgs[]) {
    
    	    ApplicationContext context = new ClasspathXmlApplicationContext("path-to-your-applicationContext.xml");
    	    IFnuDao fnuDao = (IFnuDao) context.getBean("userDAO");		
    		System.out.println(fnuDao.getStatus().size());
    	}
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default Re:

    Thanks.

    Can you check applicationContext.xml file?
    I think Iam going wrong somewhere.
    Code:
    <?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="oracleDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="hibernate.connection.driver_class">
    			<value>oracle.jdbc.driver.OracleDriver</value>
    		</property>
    		<property name="hibernate.connection.url">
    			<value>jdbc:oracle:thin:@192.168.1.18:1521:regac</value>
    		</property>
    		<property name="hibernate.connection.username">
    			<value>fnur</value>
    		</property>
    		<property name="hibernate.connection.password">
    			<value>fnur</value>
    		</property>
    		<property name="current_session_context_class">
    			<value>thread</value>
    		</property>
    		<property name="cache.provider_class">
    			<value>org.hibernate.cache.NoCacheProvider</value>
    		</property>
    		<property name="show_sql">
    			<value>true</value>
    		</property>
    		<property name="hibernate.dialect">
    			<value>org.hibernate.dialect.Oracle9Dialect</value>
    		</property>
    	</bean>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    <ref local="oracleDS"/>
    </property>
    	<property name="mappingResources">
    			<list>
    				<value>jeevan/spring/Status.hbm.xml</value>
    			</list>
    		</property>
    	</bean>
    	
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    	
    	<bean id="userDAO" class="jeevan.spring.dao.impl.FnuDAOImpl">
    		<property name="hibernateTemplate">
    			<ref local="hibernateTemplate" />
    		</property>
    	</bean>
    	
    </beans>

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well I could if I knew what was wrong or not working.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •