5

How do you create a SessionFactory using the java config?

@Bean
public SessionFactory sessionFactory(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    return sessionFactoryBean.getObject();
}

This doesnt work for some reason...it always returns null.

5 Answers 5

12

Return factory instead:

@Bean
public AbstractSessionFactoryBean sessionFactoryBean(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    return sessionFactoryBean;
}

If you need to inject SessionFactory directly somewhere in code, add this helper method:

public SessionFactory sessionFactory() {
    return sessionFactoryBean().getObject();
}

Note that the helper sessionFactory() is not annotated with @Bean - this is really important.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me but I was confused where the bean was needing to be placed. For others, if you have a class that contains an annotation with '@Configuration' I would put the '@Bean' method in there. Loaded up no problem and I was good to go.
AbstractSessionFactoryBean is deprecated
6

Tomasz is right, but I do believe that creating object instance using "new" does not feet with Spring concept:

I think you need to do it this way:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
 <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
    <prop key="hibernate.show_sql">true</prop> 
    <prop key="hibernate.hbm2ddl.auto">update</prop> 
    <prop key="hibernate.show_sql">true</prop> 
  </props>
     </property>
        <property name="annotatedClasses">
      <list>
        <value>com.vanilla.objects.Student</value>
        <value>com.vanilla.objects.Address</value>

         </list>
    </property>
  </bean>

<bean id="transactionManager" 
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

and then you can use it inside your Spring bean:

@Autowired
    SessionFactory sessionFactory;

and then inside of your method:

Session session = sessionFactory.getCurrentSession();

1 Comment

He is using Java-based container configuration, which is the new feature of Spring 3.0.x. OP's code is completely fine and idiomatic (except the problem he's facing, of course).
5

Worth noting here that Spring 3.1 introduces LocalSessionFactoryBuilder, which is expressly designed for use within @Bean methods.

http://static.springsource.org/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html

This gets around the awkward need to deal with FactoryBeans, getObject() methods, etc. FactoryBeans are excellent for use in XML, but non-ideal in @Bean methods.

Note that this new builder is Hibernate 4.1+ only.

Comments

4

Since the above answers are outdated, here's a more modern approach:

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){
    return hemf.getSessionFactory();
}

Also, if you've injected an EntityManager, you can get the current session via

Session session = entityManager.unwrap(Session.class);

Comments

1

You should call afterPropertiesSet() on the session factory after setting all the properties

So in your example it would look like:

@Bean
public SessionFactory sessionFactory(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    sessionFactoryBean.afterPropertiesSet();
    return sessionFactoryBean.getObject();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.