I'm trying to test a small snippet of code to see if I can connect to my local database with hibernate and create some tables and insert data.
package service;
import model.CategoryEntry;
import model.SubCategoryEntry;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class DatabaseConnectionTest {
@Test
public void databaseConnectionIsSuccessful() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
CategoryEntry categoryEntry = new CategoryEntry();
categoryEntry.setId(2269);
categoryEntry.setAlert(false);
categoryEntry.setLabel("Eat and Drink");
categoryEntry.setSlug("eat-drink");
List<CategoryEntry> subcategories = new ArrayList<>();
CategoryEntry subCategoryEntry1 = new CategoryEntry();
subCategoryEntry1.setId(9);
subCategoryEntry1.setAlert(false);
subCategoryEntry1.setLabel("Food");
subCategoryEntry1.setSlug("food");
subCategoryEntry1.setParent(categoryEntry);
subcategories.add(subCategoryEntry1);
categoryEntry.setSubcategories(subcategories);
try {
session.beginTransaction();
session.persist(categoryEntry);
session.getTransaction().commit();
session.close();
} catch (Exception e) {
e.getLocalizedMessage();
}
}
}
And the Entity Class:
package model;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.hibernate.annotations.Fetch;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class CategoryEntry {
@Id
private int id;
private boolean alert = false;
private String label;
private String slug;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
private CategoryEntry parent;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parent")
//@JoinColumn(name = "CATEGORY_ID")
private List<CategoryEntry> subcategories;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@ManyToOne
private SearchAgentEntry searchAgentEntry;
//getters and setters
}
The problem is that when I run the code I get this error:
java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:3038)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1763)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:972)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:799)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at service.DatabaseConnectionTest.databaseConnectionIsSuccessful(DatabaseConnectionTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
The interesting part is that when I run my project with
mvn install or mvn test
Everything works fine and I can see that I have my data saved in the database. So this makes me thing that there might be something wrong with IntelliJ rather than dependencies.
Here is my pom.xml file
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
<!--Resteasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>provided</scope>
</dependency>
<!--<dependency>
<groupId>jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.19.Final-redhat-1</version>
</dependency>-->
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
<!-- JPA & Hibernate-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.4.Final</version>
</dependency>
<!-- Database -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
From other questions I saw that this probvlem occurs mostly to not updated jpa 2.0 dependencies and hibernate < 4.0. I checked if the right jars are used and everything looks fine - javax.persistence-api is 2.2 and - hibernate is 5.3
Here is my dependency tree as well
-- maven-dependency-plugin:3.0.1:tree (default-cli) @ medbusters ---
[INFO] at.itsv.mobile.backend:medbusters:war:1.0.0-SNAPSHOT
[INFO] +- javax.enterprise:cdi-api:jar:1.0-SP1:provided
[INFO] | +- org.jboss.interceptor:jboss-interceptor-api:jar:1.1:provided
[INFO] | +- javax.annotation:jsr250-api:jar:1.0:provided
[INFO] | \- javax.inject:javax.inject:jar:1.0.0.redhat-6:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.slf4j:slf4j-jdk14:jar:1.7.25:compile
[INFO] +- org.jboss.resteasy:resteasy-client:jar:3.0.19.SP4-redhat-1:provided
[INFO] | +- org.jboss.resteasy:resteasy-jaxrs:jar:3.0.19.SP4-redhat-1:provided
[INFO] | | +- org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.0_spec:jar:1.0.0.Final-redhat-1:provided
[INFO] | | +- org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec:jar:1.0.0.Final-redhat-1:provided
[INFO] | | \- javax.activation:activation:jar:1.1.1.redhat-5:provided
[INFO] | \- org.jboss.logging:jboss-logging:jar:3.3.1.Final-redhat-1:compile
[INFO] +- at.itsv.tools:sv-logging:jar:2016.4.0.eap7:compile
[INFO] | +- at.itsv.tools:sv-utils:jar:2016.4.0.eap7:compile
[INFO] | | \- commons-beanutils:commons-beanutils:jar:1.9.3:compile
[INFO] | | \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] | +- at.itsv.tools:sv-errorhandling:jar:2016.4.0.eap7:compile
[INFO] | \- org.slf4j:slf4j-ext:jar:1.7.7.redhat-3:compile
[INFO] | \- ch.qos.cal10n:cal10n-api:jar:0.8.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.6:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.0:provided
[INFO] +- org.json:json:jar:20180130:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.7:compile
[INFO] +- org.jdom:jdom:jar:1.1:compile
[INFO] +- javax.persistence:javax.persistence-api:jar:2.2:compile
[INFO] +- org.hibernate:hibernate-core:jar:5.3.5.Final:compile
[INFO] | +- org.javassist:javassist:jar:3.23.1-GA:compile
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.8.17:compile
[INFO] | +- antlr:antlr:jar:2.7.7:compile
[INFO] | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final-redhat-1:provided
[INFO] | +- org.jboss:jandex:jar:2.0.2.Final-redhat-1:compile
[INFO] | +- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] | +- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.4.Final:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.3.4.Final:compile
[INFO] +- org.mariadb.jdbc:mariadb-java-client:jar:2.2.6:compile
[INFO] +- mysql:mysql-connector-java:jar:8.0.12:compile
[INFO] | \- com.google.protobuf:protobuf-java:jar:2.6.0:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.3:test
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.4.6:test
[INFO] | +- commons-logging:commons-logging:jar:1.2:compile
[INFO] | \- commons-codec:commons-codec:jar:1.9:test
[INFO] \- commons-io:commons-io:jar:2.5:test
The IntelliJ version is 2018.2.1
I read almost all the open questions on stakoverflow and anywhere I could relate to that problem and nothing so far helped. I'm also not super advances in IDE configurations and if the problem is in the IDE I wouldn't be able to solve it on my own.
Any Help would be appreciated