0

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

VLazzarova
  • 93
  • 1
  • 6
  • For starters, you are using two different versions of hibernate artifacts i.e. `hibernate-core` and `hibernate-entitymanager`. Can you match the versions and see if the issue still persists. – Kedar Joshi Aug 17 '18 at 13:15
  • foreignKey was introduced with JPA 2.1 like in this question https://stackoverflow.com/questions/24588860/error-javax-persistence-joincolumn-foreignkeyljavax-persistence-foreignkey-wi do you run the test in the IDE or with Maven? – Simon Martinelli Aug 17 '18 at 13:57
  • @KedarJoshi I tried with both, individually too and also with hibernate-jpa-2.1-api and no change. Tried all kind and variations with possible dependencies.. and still same issue – VLazzarova Aug 18 '18 at 14:05
  • @SimonMartinelli When i run the tests with maven everything works fine - no errors thrown and also my tables are created and my data is inserted. When I run the test from the IDE it throws me this error. I also read that foreignKey is in jpa2.1 and I made sure to use the right version. I find it weird that maven itself doesn't have a problem when running the test :D but Intellij seems to think differently :D – VLazzarova Aug 18 '18 at 14:08
  • Right click on the pom.xml in IntelliJ then choose Maven->Reimport – Simon Martinelli Aug 20 '18 at 08:21
  • Just fixed it. The problem was that Intellij was configured with a very old jdk version (1.8.1) therefore it was using javax.persistence.1.0 (O_O). I changed to the current version I use and the problem was gone. Thanks for your help! :) P.S I noticed it when importing the @Entity annotation where the import suggestion was showing me javax.persistence-1,.0 .. – VLazzarova Aug 20 '18 at 11:32

1 Answers1

0

IntelliJ was using an old jdk version 1.8 and therefore was also using an older version of JPA (1.0) regardless the dependencies I added. I changed it to the newest jdk version I have installed on my computer and the problem was solved.

File -> Project Strucutre -> in Platform Settings - SDKs and from there choose the jdk folder. 

The only dependency I needed is

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.5.Final</version>
        </dependency>

It adds automatically javax-persistance-api:2.2 and hibernate

I started working with Java recently and I have never used IntelliJ before and I am not familiar with all the configuration that have to be kept in mind when working on a project. So something little like this configuration was ignored and cost me couple of days of my work :)

VLazzarova
  • 93
  • 1
  • 6