0

Getting java.lang.IllegalArgumentException: Property 'sessionFactory' is required in my spring boot + hibernate 5 application. I try to autowire it like this

@Autowired
    public void setupSessionFactory(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
        getHibernateTemplate().setCheckWriteOperations(false);
    }

And also i tried to add stuff like this to my config file

 @Bean
    public HibernateJpaSessionFactoryBean getSessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }

    @Bean
    public SessionFactory sessionFactory(){
        return new LocalSessionFactoryBean().getObject();
    }

    @Bean
    public HibernateTransactionManager transactionManager(SessionFactory sf) {
        return new HibernateTransactionManager(sf);
    }

My application.yml file

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/db
    driver-class-name: org.postgresql.Driver
    username: postgres
    password: password
  jpa:
    hibernate:
      ddl-auto: update

security:
  basic:
    enabled: false

Any ideas how to solve this?

MolecularMan
  • 227
  • 2
  • 16

1 Answers1

0

It could be because return new LocalSessionFactoryBean().getObject(); returns null.

This method simply returns internal field value:

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

which is null be default.

Session factory needed to be configured: dataSource should be setup, package to scan, hibernate properties. Something like that:

   @Bean
   public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
      sessionFactory.setDataSource(restDataSource());
      sessionFactory.setPackagesToScan(
        new String[] { "org.baeldung.spring.persistence.model" });
      sessionFactory.setHibernateProperties(hibernateProperties());

      return sessionFactory;
   }
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • error changed to: Description: Field ownerRepository in com.jgang.realestate.service.OwnerServiceImpl required a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration. – MolecularMan Feb 12 '17 at 18:05
  • I have no idea what is `com.jgang.realestate.service.OwnerServiceImpl`, but it is definitely different problem, while question's problem is solved – Andremoniy Feb 12 '17 at 18:06
  • and btw, i can use application.yml to setup datasource and hibernate properties? – MolecularMan Feb 12 '17 at 18:09
  • OwnerServiceImpl is service, which use dao – MolecularMan Feb 12 '17 at 18:10
  • Do you think I see how does this class look like? Too many questions in one topic. Create new question and describe there new problem. – Andremoniy Feb 12 '17 at 18:12
  • Service use dao like @Autowired private OwnerRepository ownerRepository; The case is that anyother solution I tried before alternated these 2 errors – MolecularMan Feb 12 '17 at 18:16