首页 > 解决方案 > Cannot find class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] in xml configuration file

问题描述

I'm learning how to use hibernate integration in the spring project and try to follow the example below: https://www.journaldev.com/3524/spring-hibernate-integration-example-tutorial

The tutorial has pom.xml and other sample files, but I use the start.spring.io initializer to create a pom.xml file and try to integrate my path with the example of the tutorial.

So when I came to create context.xml and createdbean, as shown in the tutorial, idea could not find any class in the library:

<bean id = "hibernate3AnnotatedSessionFactory"
class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name = "dataSource" ref = "dataSource" />

Thus, the hibernate3 package is unavailable to me, and I changed the packagehibernate3 to hibernate5, but in this case the annotation package is not available, and not available the AnnotationSessionFactoryBean class to.


Question

Can I use the `AnnotationSessionFactoryBean' in hibernate 5.v, or this class already outdated?


pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <hibernate-version>5.3.7.Final</hibernate-version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-version}</version>
    </dependency>

    <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate-version}</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="hibernate5AnnotatedSessionFactory"
      class="org.springframework.orm.hibernate5.... - AnnotatedSessionFactory not avaiilable

标签: javaspringhibernate

解决方案


我认为您可以使用org.springframework.orm.hibernate5.LocalSessionFactoryBeanand its'annotatedClasses或属性。annotatedPackagespackagesToScan


推荐阅读