Skip to content
Advertisement

Unsatisfied dependency through ‘sessionFactory’; No qualifying bean of type ‘org.hibernate.SessionFactory’ available

I am creating a spring-mvc and hibernate webapp. I am trying to test text read in the book by creating project (hence using xml config). This type of question is asked before but I can’t get my project to work with answers offered. I get following error when I hit the end-point :

[WARNING] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryController': Unsatisfied dependency expressed through field 'categoryService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryServiceImpl': Unsatisfied dependency expressed through field 'categoryDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)}
[ERROR] Context initialization failed

Here is @Configuration class

@Configuration
@ComponentScan(basePackages= {"pizzaml.rest", "pizzaml.service", "pizzaml.dao"})
@EnableWebMvc
public class AppConfig {
    
    @Bean(name="sessionFactory")
    public SessionFactory sessionFactory( @Qualifier("dataSource") DataSource dataSource) throws MalformedURLException {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setConfigLocation(new FileUrlResource("/hibernate-cfg.xml"));
        return sessionFactory.getObject();
    }
    
    @Bean(name="dataSource")
    public DataSource dataSource( @Value("{db.user}") String user, @Value("{db.pass}") String pass, @Value("{db.url}") String url, @Value("{db.driverclass}") String driverClass) {
        BasicDataSource datasource = new BasicDataSource();
        datasource.setDriverClassName(driverClass);
        datasource.setUsername(user);
        datasource.setPassword(pass);
        datasource.setUrl(url);
        
        return datasource;
    }
    
}

Here is hibernate-cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        
    <hibernate-configuration>
        
        <session-factory>
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">update</property>
            <mapping resource="pizzaml/entity/Category.hbm.xml"></mapping>
        </session-factory>
    
    </hibernate-configuration>

DAO-

@Repository
public class CategoryDAOImpl implements CategoryDAO {
    
    @Autowired
    @Qualifier("sessionFactory")
    SessionFactory sessionFactory;

    @Override
    public String addCategory(String categoryname) throws BusinessProcessException {
        
        Session session = sessionFactory.openSession();
        
        Query query = session.getNamedQuery("Category.byName");
        List<Category> list = query.list();
        
        if(Objects.nonNull(list) && (!list.isEmpty()) )
            throw new BusinessProcessException("Category already exists!");
        
        Category category = new Category();
        category.setName(categoryname);
        
        session.save(category);
        
        session.flush();
        
        session.close();
        
        return AppCodes.SUCCESS;
    }

}

pom.xml

  <dependencies>
  
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.22.Final</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.8.0</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.11.3</version>
    </dependency>
    
    
  </dependencies>
  
  <build>
    
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>    
        </plugin>
        
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.3</version>
            <configuration>
                <outputDirectory>./target</outputDirectory>
            </configuration>
        </plugin>
        
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>4500</port>
                <path>/</path>
            </configuration>
        </plugin>
        
    </plugins>

Advertisement

Answer

I did a workaround for this.

Instead of doing localSessionFactoryBean.getObject() to obtain hibernate session factory.

I used hibernate’s configuration class to create session factory object. The only problem was I wanted to give my own datasource. For this I created a class that implemented ConnectionProvider interface. And set property – hibernate.connection.provider_class in hibernate.cfg.xml.

And it started working.

I agree with other comments though, hbm.xmls are old way of doing things. I will learn annotations and start using them in future projects.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement