Announcement Announcement Module
Collapse
No announcement yet.
No Hibernate Session bound to thread, and configuration does not allow creation of no Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No Hibernate Session bound to thread, and configuration does not allow creation of no

    Hi All,

    I need your expert help. I am new to Spring & trying to integrate Hibernate 3 with Spring using XML configeration rather than annotation. Here I am trying to add new record in Employee table.
    Below is the code for the same

    1. Employee class - java object for employee table
    public class Employee {
    private int empId = 0;
    private String firstName = "default";
    private String lastName = "default";
    private int age = 0;
    private int deptId = 0;
    private String Cadre = "B11";
    private double sal = 0.00;
    private Date DOJ = null;
    // all getter & setter methods

    2. Employee.hbm.xml - hibernate mapping file
    <hibernate-mapping>
    <class name="com.lnt.spring.dataccess.hibernate.Employee" table="Employee">
    <id name="empId" type="int" column="EmpId">
    <generator class="assigned" />
    </id>

    <property name="firstName">
    <column name="EmpFirstName" />
    </property>
    ........................

    3.EmpDAO -
    public interface EmpDAO {
    int getCount();
    List<Employee> selectEmp();
    Employee getEmp(int empId);
    void addEmp(Employee e);
    void setSal(int id, int sal);
    }

    4.EmpDAOImpl -@Repository
    public class EmpDAOImpl implements EmpDAO {
    private SessionFactory sessionFactory;

    @Autowired
    public EmpDAOImpl(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    }

    private Session currentSession() {

    return sessionFactory.getCurrentSession();
    }
    @Override
    @Transactional
    public void addEmp(Employee e) {
    currentSession().save(e);
    }
    //Other override method

    5.HibConfig.xml - Spring config file
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDrive r" />
    <property name="url"
    value="jdbc:sqlserver://localhost:1433;databaseName=Spring3" />
    <property name="username" value="sa" />
    <property name="password" value="Newuser123" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
    </bean>

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
    <list>
    <value>com/lnt/spring/dataccess/hibernate/Employee.hbm.xml</value> <!-- Can Add multiple mapping file for each objects -->
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLS erverDialect</prop>
    </props>
    </property>
    </bean>

    <bean id="empDaoImpl" class="com.lnt.spring.dataccess.hibernate.EmpDAOIm pl">
    <!-- <property name="sessionFactory" ref="sessionFactory" /> -->
    <constructor-arg ref="sessionFactory" ></constructor-arg>
    </bean>

    6. ExecuteMe class to execute it
    public class ExecuteMe {

    /**
    * @param args
    */
    @Transactional
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext ac = new ClassPathXmlApplicationContext("com/lnt/spring/dataccess/hibernate/HibConfig.xml");
    EmpDAO e = (EmpDAO)ac.getBean("empDaoImpl");
    System.out.println(e.toString());

    Employee emp = new Employee();
    emp.setEmpId(7);
    emp.setFirstName("Andrea");
    emp.setLastName("Robbert");
    emp.setCadre("B11");
    emp.setAge(35);
    emp.setDeptId(2);
    emp.setSal(95000);

    Date d = new Date(2);
    emp.setDOJ(d);

    e.addEmp(emp);
    }

    }


    When I am trying to execute it I am getting -
    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA pplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    com.lnt.spring.dataccess.hibernate.EmpDAOImpl@d589 39
    Exception in thread "main" org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
    at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:544)
    at com.lnt.spring.dataccess.hibernate.EmpDAOImpl.curr entSession(EmpDAOImpl.java:37)
    at com.lnt.spring.dataccess.hibernate.EmpDAOImpl.addE mp(EmpDAOImpl.java:76)
    at com.lnt.spring.dataccess.hibernate.ExecuteMe.main( ExecuteMe.java:34)


    I found that SessionFactory is getting set properly but when trying to get session via sessionFactory.getCurrentSession() I am not getting any session & throwing the exception. Can you please tell me what step I am missing here ? Need your help urgently.

    Thanks
    Shree

  • #2
    Please use [ code][/code ] tags when posting code/xml/stacktraces, that way it remains readable ...

    Also please use the search as this question has been answered numerous times before.

    In short configure transaction management without it it will not work.

    Comment


    • #3
      Thanks , I actually followed -
      http://forum.springsource.org/showth...creation+of+no

      & added transaction management as follows
      Code:
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      	xmlns:aop="http://www.springframework.org/schema/aop"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans 
      	http://www.springframework.org/schema/beans/spring-beans.xsd
      	http://www.springframework.org/schema/tx
      	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      	http://www.springframework.org/schema/aop
      	http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
      
      	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      		<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
      		<property name="url"
      			value="jdbc:sqlserver://localhost:1433;databaseName=Spring3" />
      		<property name="username" value="sa" />
      		<property name="password" value="Newuser123" />
      		<property name="initialSize" value="5" />
      		<property name="maxActive" value="10" />
      	</bean>
      
      	<bean id="sessionFactory"
      		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="dataSource" ref="dataSource" />
      		<property name="mappingResources">
      			<list>
      				<value>com/lnt/spring/dataccess/hibernate/Employee.hbm.xml</value> <!-- Can Add multiple mapping file for each objects -->
      			</list>
      		</property>
      		<property name="hibernateProperties">
      			<props>
      				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
      			</props>
      		</property>
      	</bean>
      
      	<bean id="empDaoImpl" class="com.lnt.spring.dataccess.hibernate.EmpDAOImpl">
      		<!-- <property name="sessionFactory" ref="sessionFactory" /> -->
      		<constructor-arg ref="sessionFactory"></constructor-arg>
      	</bean>
      
       	<bean id="transactionManager"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory" ref="sessionFactory"></property>
      	</bean>
      
      	<tx:advice id="txAdvice" transaction-manager="transactionManager">
      		<tx:attributes>
      			<tx:method name="create" />
      			<tx:method name="update" />
      			<tx:method name="delete" />
      			<tx:method name="find" read-only="true" />
      		</tx:attributes>
      	</tx:advice>
      
      	<aop:config>
      		<aop:pointcut id="ServiceOperation"
      			expression="execution(* com.lnt.spring.dataccess.hibernate.EmpDAOImpl.*(..))" />
      		<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOperation" />
      	</aop:config> 
      
      	<!-- <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> -->
      </beans>
      & when I am running my code I am getting following error -
      Code:
      Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 48 in XML document from class path resource [com/lnt/spring/dataccess/hibernate/HibConfig.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:advice'.
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
      	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
      	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
      	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
      	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
      	at com.lnt.spring.dataccess.hibernate.ExecuteMe.main(ExecuteMe.java:18)

      I checked my classpath also - Here is what I have .
      Code:
      <classpathentry kind="src" path="src"/>
      	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.beans_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.context_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.core_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.expression_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/commons-logging.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.asm_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/aspectjrt-1.6.7.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/antlr-2.7.7.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/aopalliance.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.aop_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/spring.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/cglib-nodep-2.1_3.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/sqljdbc4.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.jdbc_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/commons-dbcp-1.4.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/commons-pool-1.6.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/hibernate3.jar"/>
      	<classpathentry kind="lib" path="C:/Program Files/springsource/sts-2.8.1.RELEASE/plugins/org.springframework.orm_3.1.0.RC1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/dom4j-1.6.1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/asm-3.1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/aspectjweaver.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/commons-collections-3.2.1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/hibernate-annotations-3.3.0.ga.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/hibernate-commons-annotations-3.3.0.ga.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/jstl.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/jta-1.1.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/log4j-1.2.14.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/servlet-api.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/slf4j-api-1.6.2.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/spring-web.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/spring-webmvc.jar"/>
      	<classpathentry kind="lib" path="D:/Spring/Workspace/lib/standard.jar"/>
      	<classpathentry kind="output" path="bin"/>
      Do I have to include anything else ? I have not changed anyother code. Please help me. I am getting confused

      Thanks
      Shree

      Comment


      • #4
        For starters use a final spring version not a RC and next to that you seem to be mixing different versions (3.1.0.RC1 and some undetermined version so not sure what you are trying to do). Next you seem to be using 3.1 (at least are attempting to ) so fix the xsd (you are now pointing to 3.0, 2.0 and an undetermined version).

        Comment

        Working...
        X