Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:57:58+00:00 2026-06-05T05:57:58+00:00

I use Spring with Hibernate and get always a NPE for the sessionFactory Object.

  • 0

I use Spring with Hibernate and get always a NPE for the sessionFactory Object.

My config file:

@Configuration
public class HibernateConfiguration {

@Bean
public AnnotationSessionFactoryBean sessionFactory() {
    Properties props = new Properties();
    props.put("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
    props.put("hibernate.format_sql", "true");
    props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    props.put("hibernate.connection.password", "xxx");
    props.put("hibernate.connection.url", "jdbc:mysql://localhost/Market");
    props.put("hibernate.connection.username", "philipp");

    AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
    bean.setAnnotatedClasses(new Class[] { xxx.class, xxx.class, xxx.class });
    bean.setHibernateProperties(props);
    bean.setSchemaUpdate(true);
    return bean;
}





@Bean
public HibernateTransactionManager transactionManager() {
    return new HibernateTransactionManager(sessionFactory().getObject());
}

@Bean
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
    return new PersistenceExceptionTranslationPostProcessor();
}

}

My DAOImpl class:

@Repository("xxx")
public class xxxDAOImpl implements xxxDAO {

private SessionFactory sessionFactory;

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

private Session currentSession() {
    return sessionFactory.getCurrentSession();
}
...

Testcase:

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class TestxxxDAOImpl {

@Test
@Transactional
public void testInsertxxx() throws Exception {

    xxxDAO xxxDAO = new xxxDAOImpl();
    xyz xyz = new xyz();
    xxxDAO.insert(xyz);
    assertNotNull(xyz.getId());
    }

}

app-context.xml

<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
    http://www.springframework.org/schema/jdbc   http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="xxx.config" />
<context:component-scan base-package="xxx.dao" />
<context:annotation-config></context:annotation-config>

test-context.xml

<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<import resource="classpath:/META-INF/spring/app-context.xml" />

I get always an NPE from the currentSession when the test calls the insert method.

public void insert(xxx xxx) {
    currentSession().save(xxx);
}


private Session currentSession() {
    return sessionFactory.getCurrentSession();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:58:00+00:00Added an answer on June 5, 2026 at 5:58 am

    The very first thing to understand when working with Spring is that Spring dependency injection only works for beans obtained from the application context, not for beans created with new.

    In Spring-enabled unit tests you configure application context using @ContextConfiguration, for example, as follows (works in Spring 3.1, in previous versions @ContextConfiguration doesn’t take @Configuration classes directly, therefore you’ll have to create XML configuration file):

    @Configuration 
    public class HibernateConfiguration { 
        // Add your DAO to application context
        @Bean
        public xxxDAO xxxDAO() {
            return new xxxDAOImpl();
        }
        ...
    }
    
    // Configure application context using the given @Configuration class
    @ContextConfiguration(classes = HibernateConfiguration.class)   
    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestxxxDAOImpl {
        // Obtain DAO from the application context    
        @Autowired xxxDao xxxDao;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Spring MVC 3.0 and JSP. I have an object: public class ObjectWrapper
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
my java project with spring and hibernate,can't get the sessionFactory.it's null all the time
in my attempt to learn a bit faster the use of spring and hibernate
I work on Spring MVC + Hibernate application, use MySQL (ver. 5.0.51a) with the
I want to create a web app that will use wicket, hibernate and spring
In our Spring application we use clustered Hibernate Search with ActiveMQ which sets up
I'm trying to use resteasy to serve out some entities fetched by spring-hibernate. I've
I use GWT along with Spring/Hibernate/AOP. I use an Aspect to send notification emails.
Long story below short question: How can I get Spring's NamedParameterJDBCTemplate join Hibernate's session?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.