Home > Mobile >  SessionFactory addAnnotatedClass method not found
SessionFactory addAnnotatedClass method not found

Time:04-07

I am following a spring course covering hibernate and I think I'm missing something in my pom.xml. Java cannot find the addAnnotatedClass method. The class I'm watching isn't using maven and I didn't want to download all of the jars manually. The documentation has the method here that isn't deprecated.

package hibernate;

import hibernate.entity.Student;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class CreateStudentDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //create session factory
        SessionFactory factory =  new Configuration()
                .configure("hibernate.cfg.xml")
                .buildSessionFactory();
        factory.addAnnotatedClass(Student.class);
        //create session
        Session session = factory.getCurrentSession();
        try {
            Student student = new Student("A", "B", "[email protected]");
            
            //start transaction
            session.beginTransaction();
            //save object
            session.save(student);
            //commit transaction
            session.getTransaction().commit();
            
        }finally {
            session.close();
        }
    }

}

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com</groupId>
    <artifactId>hibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.googlecode.soundlibs</groupId>
            <artifactId>basicplayer</artifactId>
            <version>3.0.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.25.Final</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
         <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
    </dependencies>
</project>

Edit: Correct Pom File

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com</groupId>
    <artifactId>hibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.googlecode.soundlibs</groupId>
            <artifactId>basicplayer</artifactId>
            <version>3.0.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
         <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
    </dependencies>
</project>

CodePudding user response:

May be your hibernate version is not matching.

According to following post you need to use hibernate-core 3.6.0 or higher than that.

java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass

  • Related