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/):
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.