Announcement Announcement Module
Collapse
No announcement yet.
Hibernate sessionFactory not being instanciated Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 ...

    Hi,
    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.
    spring_config.xml
    <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>

    <bean id="peopleDAO" class="com.norricorp.pm.PeopleDAO_Hibernate">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    hibernate.cfg.xml
    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost/propman
    </property>
    <property name="hibernate.connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <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="com.norricorp.pm.People"/>
    </session-factory>
    </hibernate-configuration>

    PeopleDAO_Hibernate.java
    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".

    unitTest.java
    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.

    Regards,
    John
    Last edited by norricorp; Mar 6th, 2013, 10:50 AM.
Working...
X