-1

I'm trying to make a simple app using Hibernate, but I'm getting this error and not sure why.

I'm using IntelliJ, made Maven Project and generated model from database:

this is my 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.nemanjagajic</groupId>
    <artifactId>FacultyJPA</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.15.Final</version>
        </dependency>
    </dependencies>

</project>

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="FacultyJPAPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>model.Student</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/faculty"/>
            <property name="hibernate.connection.autocommit" value="false"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="nemanja96"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.connection.CharSet" value="utf8"/>
            <property name="hibernate.connection.characterEncoding" value="utf8"/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

basic JPAUtil:

package utils;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtil {
    static EntityManagerFactory factory;

    static {
        factory = Persistence.createEntityManagerFactory("FacultyJPAPersistenceUnit");
    }

    public static EntityManager getEntityManager() {
        return factory.createEntityManager();
    }
}

and when I run it

public class Application {

    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("John");
        student.setLastName("Dee");

        EntityManager em = JPAUtil.getEntityManager();
        em.persist(student);
    }

}

I'm getting the error above.

Not sure what I'm doing wrong and can't seem to configure simple project using hibernate whatever I try. If someone could tell me what is the problem or pass me the link of some tutorial creating simple project using hibernate that is not outdated and is working.

nemanjagajic
  • 39
  • 12
  • I wonder what happens when you put the title of your post in to the search bar above? https://stackoverflow.com/questions/42803308/how-to-solve-caused-by-java-lang-nosuchmethoderror-javax-persistence-table-ind –  Mar 18 '18 at 14:26
  • https://stackoverflow.com/questions/26616723/java-lang-nosuchmethoderror-javax-persistence-table-indexesljavax-persistenc?s=2|196.8829 –  Mar 18 '18 at 14:26
  • https://stackoverflow.com/questions/44300844/nosuchmethoderror-javax-persistence-table-indexesljavax-persistence-index?s=4|192.5451 –  Mar 18 '18 at 14:27
  • I've read them, didn't solve problem for me so i posted – nemanjagajic Mar 18 '18 at 14:40
  • NoSuchMethodError is basic java. You have a version of a class that does not have that method. So you have a JPA API v2.0 or earlier jar in the classpath. End of. Same applies to ALL of those duplicated questions –  Mar 18 '18 at 14:48
  • Alright, but after reading all the rest, I'm still not sure how to resolve the problem. Should I copy or remove some jar, change classpath or something else? – nemanjagajic Mar 18 '18 at 15:31
  • Remove the jpa api v2.0 jar from class path obviously –  Mar 18 '18 at 15:34
  • Oh... ok now I realize you're right and it was duplicate post after all. Thanks for help. – nemanjagajic Mar 18 '18 at 15:45

1 Answers1

0

Can you please provide the stack trace for the exception/error?

Look at the stack trace ... If the exception appears when calling a method on an object in a library, you are most likely using separate versions of the library when compiling and running. Make sure you have the right version both places.

If the exception appears when calling a method on objects instantiated by classes you made, then your build process seems to be faulty. Make sure the class files that you are actually running are updated when you compile.

i.e

  • Check the class containing the method is in class path.

  • Check if you have added new version of JAR and the method is compatible.

Mahesh_Loya
  • 2,743
  • 3
  • 16
  • 28
  • It's the first case: at javax.persistence.Persistence.createEntityManagerFactory. How exactly to make sure I have the right versions and where? – nemanjagajic Mar 18 '18 at 14:38