1

Here is my test function/class (I get the error at "session.save(usr)" line):

public class MainToTest {
public static void main(String[] args) {
Transaction trans=null;
    Session session= dbSingelton.getSessionFactory().openSession();
    trans=session.beginTransaction();
    User usr=new User (  "email",  "firstName",  "lastName",  "password");
    session.save(usr);
    trans.commit();
}

Here is the stack trace:

Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1236)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:828)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:248)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:230)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at db.dbSingelton.getSessionFactory(dbSingelton.java:12)
at db.MainToTest.main(MainToTest.java:12)

Here is my user class:

@Entity
@Table(name="Student")
public class User implements Serializable {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    @Column(name="Email", unique = true, nullable = false)
    private String email;
    @Column(name="First Name")
    private String firstName;
    @Column(name="Last Name")
    private String lastName;
    @Column(name="Password")
    private String password;
    //setters, getters and constructors
}

Here is my hibernate.cfg.xml ( I have used different types of databases (Mostly on remote servers like Azure's) with their proper configuration, and still got the very same error):

<hibernate-configuration>
<session-factory >
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">%=4b[E6c</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
    <property name="hibernate.connection.databaseName">SocialDB</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <mapping class="db.User" />

</session-factory>
Here are my referenced libraries (Extracted from latest version of hibernate (5.4) downloaded from https://hibernate.org/):

enter image description here

I have found so many topics where the OP solved the problem by using "hibernate-jpa-2.1" (instead of 2.0), and when I added "hibernate-jpa-2.1" next to the libraries above I had the very same exception, however, when I removed "javax.persistence-api-2.2" from the referenced libraries (Since "hibernate-jpa-2.1" had its own persistence classes), I got this exception instead: java.lang.NoClassDefFoundError: javax/persistence/TableGenerators

(There is TableGenerator (Singular) in "hibernate-jpa-2.1" but not TableGenerators (Plural) which only exists in javax.persistence-api-2.2.jar).

When I do not use hibernate-jpa at all I get the same exception mentioned in the title.

4
  • 1
    Does this answer your question? Hibernate 5 - java.lang.NoSuchMethodError: javax.persistence.Table.indexes() Commented Nov 9, 2020 at 0:01
  • @Ramy670 What mysql database version do you use? Commented Nov 9, 2020 at 11:27
  • 1
    @DavidBrossard Unfortunately it doesn't like I mentioned in the last lines in the post. Commented Nov 9, 2020 at 12:21
  • @SternK I tried many different versions. Commented Nov 9, 2020 at 12:21

1 Answer 1

0

The problem was solved by letting Netbeans download the libraries itself (By right clicking "Libraries" -> Add Library and choosing Hibernate 4.3 JPA, instead of downloading them from hibernate website.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.