Announcement Announcement Module
Collapse
No announcement yet.
Simple issue is acting as a blocker: No SessionFactory specified(IllegalArgumentExce) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple issue is acting as a blocker: No SessionFactory specified(IllegalArgumentExce)

    I have a spring hibernate code. To my knowledge the code is correct. I am not able to identify where it is going wrong. In the DAO class I declared SessionFactory variable. It says
    Caused by: java.lang.IllegalArgumentException: No SessionFactory specified
    at org.springframework.util.Assert.notNull(Assert.jav a:112)
    at org.springframework.orm.hibernate3.SessionFactoryU tils.doGetSession(SessionFactoryUtils.java:283)
    EmployeeDaoImpl
    Code:
    package core.dao;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.orm.hibernate3.SessionFactoryUtils;
    import core.hibernate.Employee;
    
    public class EmployeeDaoImpl implements EmployeeDAO {
    	SessionFactory sessionFactory;
    	public SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
    
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
    
    	Session session = SessionFactoryUtils.getSession(this.sessionFactory, true);
    	
    	@Override
    	public Employee getEmployee(String id) 
    	{
    		Employee employee = (Employee)session.get(Employee.class, id);
    		session.close();
    		return employee;
    	}
    
    	@Override
    	public void saveOrUpdate(Employee employee) 
    	{
    		session.save(employee);
    	}
    }
    application.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	                    http://www.springframework.org/schema/beans/spring-beans.xsd
    		                http://www.springframework.org/schema/context 
    		                http://www.springframework.org/schema/context/spring-context-3.0.xsd
    		                http://www.springframework.org/schema/tx 
    		                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    		                http://www.springframework.org/schema/util 
    		                http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    	    <property name="driverClassName" value="org.postgresql.Driver"/>
    	    <property name="url" value="jdbc:postgresql://localhost:5432/mydb"/>
    	    <property name="username" value="postgres"/>
    		<property name="password" value="venkat7"/>
    	</bean>
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="mappingResources">
    			<list>
    				<value>/employee.hbm.xml</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<value>hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect</value>
    		</property>
    	</bean>
    
    	<bean id="employeeDao" class="core.dao.EmployeeDaoImpl">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    		                
    </beans>
    Client Code
    Code:
    package core.client;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.Resource;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import core.dao.EmployeeDao2;
    import core.dao.EmployeeDaoImpl;
    import core.hibernate.Employee;
    
    public class App {
    
    	public static void main(String[] args) {
                //Resource resource = new FileSystemResource("Spring-Hibernate.xml");
                //BeanFactory factory = new XmlBeanFactory(resource);
    		    ApplicationContext factory = new FileSystemXmlApplicationContext("application.xml");
                System.out.println("Start ...");
                Employee employee = new Employee();
                employee.setId("12345");
                employee.setName("ABC");
                employee.setAge(20);
                employee.setSalary(15000);
                System.out.println("Values are set to POJO ...");
                //EmployeeDao2 employeeDao = (EmployeeDao2)factory.getBean("employeeProxy");
                EmployeeDaoImpl employeeDao = (EmployeeDaoImpl)factory.getBean("employeeDao");
                System.out.println("Bean retrived from bean factory ...");
                
                employeeDao.saveOrUpdate(employee);        
                System.out.println("Data has been saved");
                Employee empResult = employeeDao.getEmployee("1234");       
                System.out.println("Employee results " +empResult);
    
    	}
    	
    
    }
    Error Stack Trace
    Caused by: java.lang.IllegalArgumentException: No SessionFactory specified
    at org.springframework.util.Assert.notNull(Assert.jav a:112)
    at org.springframework.orm.hibernate3.SessionFactoryU tils.doGetSession(SessionFactoryUtils.java:283)
    at org.springframework.orm.hibernate3.SessionFactoryU tils.getSession(SessionFactoryUtils.java:202)
    at core.dao.EmployeeDaoImpl.<init>(EmployeeDaoImpl.ja va:18)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:100)
    ... 18 more

  • #2
    No it isn't correct... Your code is flawed... Never use the session as an instance variable in your dao...

    Also don't use SessioNFactoryUtils, simply use SessionFactory.getCurrentSession. Which in turn will give you an exception indicating you haven't configured transactions nor a TransactionManager.

    Comment

    Working...
    X