Announcement Announcement Module
No announcement yet.
Hibernate sessionFactory not being instanciated Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate sessionFactory not being instanciated

    I realised that the spring_config file in my resources directory is not in the resources directory of the tests. So copied there and now have different error so hopefully progress. And "org.hibernate.cfg.Configuration" needs to be "org.hibernate.cfg.AnnotationConfiguration". So making progress ...

    I am new to Spring and hibernate. I took an old web app that used JDBC and have been making changes to it. I added hibernate and that worked. I added spring to create an instance of a class and have now moved on to using spring to create an instance of a SessionFactory (before I was doing it manually).
    But my unit tests are showing Sessionfactory is null.
    This is what I have done.
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.Configuration" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />

    <bean id="peopleDAO" class="">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="hibernate.connection.url">
    <property name="hibernate.connection.driver_class">
    <property name="hibernate.connection.username">fred</property>
    <property name="hibernate.connection.password">XXXXX</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MyS QLInnoDBDialect </property>
    <property name="hibernate.show_sql">false</property>
    <mapping class=""/>
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
    System.out.println("into setSessionfactory");
    this.sessionFactory = sessionFactory;

    // now go on to use session factory
    if (this.sessionFactory == null)
    System.out.println("this factory is null");

    Two things - when I run the tests, I see factory is null message but I do not see "into setSessionFactory".
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring_config.xml"});

    I have read through several books, several sets of examples and all are slightly different. I am looking for why the above does not work rather than completely change your approach. I amy need to completely change my approach if the above is utterly wrong but I would like to know why it is utterly wrong. And I am not sure it is.

    Last edited by norricorp; Mar 6th, 2013, 09:50 AM.