我正在尝试测试一小段代码,以查看是否可以使用hibernate连接到本地数据库并创建一些表并插入数据。
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();
}
}
}
和实体类:
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
}
问题是,当我运行代码时,出现此错误:
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)
有趣的部分是,当我使用
运行项目时mvn install or mvn test
一切正常,我可以看到我的数据已保存在数据库中。因此,这使我觉得IntelliJ可能有问题,而不是依赖项。
这是我的pom.xml文件
<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>
从其他问题中,我看到这个问题主要发生在未更新的jpa 2.0依赖关系和<4.0的休眠状态。我检查了是否使用了正确的罐子,一切看起来都很好 -javax.persistence-api是2.2和 -休眠状态为5.3
这也是我的依赖树
-- 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
IntelliJ版本是2018.2.1
我阅读了关于stakoverflow的几乎所有未解决的问题,以及与该问题有关的任何地方,到目前为止没有任何帮助。 我在IDE配置方面也不是很先进,如果问题出在IDE中,我将无法自行解决。
任何帮助将不胜感激
答案 0 :(得分:0)
IntelliJ使用的是旧的jdk版本1.8,因此,无论我添加的依赖项如何,都还使用了较旧的JPA(1.0)版本。我将其更改为计算机上已安装的最新jdk版本,此问题已解决。
File -> Project Strucutre -> in Platform Settings - SDKs and from there choose the jdk folder.
我唯一需要的依赖是
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.5.Final</version>
</dependency>
它会自动添加javax-persistance-api:2.2并进入休眠状态
我最近开始使用Java,但我以前从未使用过IntelliJ,而且我对项目中必须记住的所有配置并不熟悉。因此,有点像这种配置的东西被忽略了,并且花了我几天的时间:)