NoSuchMethodError:javax.persistence.JoinColumn.ForeignKey

时间:2016-06-12 13:34:01

标签: java hibernate maven jpa

当我试图进行测试时(实际上它是空的,我用它来控制我测试的构建:))我收到错误:

selectAll

我的POM.xml看起来像这样:

Caused by: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2898)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1742)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:906)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:876)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:319)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)

和persistence.xml一样:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>StarterKit</groupId>
   <artifactId>Gielda</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <properties>
         <java.version>1.8</java.version>
         <spring.framework.version>4.2.5.RELEASE</spring.framework.version>
         <junit.version>4.12</junit.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <dependencies>
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
         </dependency>
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
         </dependency>
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
         </dependency>
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
         </dependency>
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
         </dependency>
         <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
         </dependency>
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.springframework/spring-tx -->
         <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.2.5.RELEASE</version>
         </dependency>
         <!--     http://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
         <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>1.10.1.RELEASE</version>
         </dependency>

         <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.2</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/javax.el/el-api -->
         <dependency>
                <groupId>javax.el</groupId>
                <artifactId>el-api</artifactId>
                <version>2.2</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.glassfish.web/javax.el -->
         <dependency>
                <groupId>org.glassfish.web</groupId>
                <artifactId>javax.el</artifactId>
                <version>2.2.6</version>
         </dependency>

         <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
         <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.1.0.Final</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
         <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>5.1.0.Final</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
         <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.2.4.Final</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
         <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-annotations</artifactId>
                <version>3.5.6-Final</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate -->
         <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate</artifactId>
                <version>3.5.4-Final</version>
                <type>pom</type>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-commons-annotations -->
         <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-commons-annotations</artifactId>
                <version>3.2.0.Final</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
         <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.19</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
         <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.19</version>
         </dependency>
         <!--     http://mvnrepository.com/artifact/org.jadira.usertype/usertype.core -->
         <dependency>
                <groupId>org.jadira.usertype</groupId>
                <artifactId>usertype.core</artifactId>
                <version>5.0.0.GA</version>
         </dependency>

         <dependency>
                <groupId>org.jinq</groupId>
                <artifactId>jinq-jpa</artifactId>
                <version>1.6.2</version>
         </dependency>
         <!-- http://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
         <dependency>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <version>2.3.3</version>
         </dependency>
         <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.1-api</artifactId>
                <version>1.0.0.Final</version>
         </dependency>
         <dependency>
                <groupId>ma.glasnost.orika</groupId>
                <artifactId>orika-core</artifactId>
                <version>1.4.5</version>
         </dependency>

   </dependencies>

   <dependencyManagement>
         <dependencies>
                <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-aop</artifactId>
                       <version>${spring.framework.version}</version>
                </dependency>
                <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-beans</artifactId>
                       <version>${spring.framework.version}</version>
                </dependency>
                <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-context</artifactId>
                       <version>${spring.framework.version}</version>
                </dependency>
                <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-context-support</artifactId>
                       <version>${spring.framework.version}</version>
                </dependency>
                <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-core</artifactId>
                       <version>${spring.framework.version}</version>
                </dependency>
                <dependency>
                       <groupId>junit</groupId>
                       <artifactId>junit</artifactId>
                       <version>${junit.version}</version>
                       <scope>test</scope>
                </dependency>
                <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-test</artifactId>
                       <version>${spring.framework.version}</version>
                       <scope>test</scope>
                </dependency>
         </dependencies>
   </dependencyManagement>

   <build>
         <plugins>
                <plugin>
                       <artifactId>maven-compiler-plugin</artifactId>
                       <configuration>
                             <source>1.8</source>
                             <target>1.8</target>
                       </configuration>
                </plugin>
         </plugins>
   </build>

</project>

我在其他主题中看到它可能是由javax.persistence或hibernate的版本引起的,但在我看来,我有两个:版本2.1的持久性和比4.3更新的Hibernate。你知道我可能仍然会收到这个错误吗?

我的2个实体:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Gielda">
    <class>pl.spring.demo.entity.KursEntity</class>
    <class>pl.spring.demo.entity.SpolkaEntity</class>
</persistence-unit>
</persistence>

package pl.spring.demo.entity;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Table(name="kursy")
@NamedQuery(name="KursEntity.findAll", query="SELECT k FROM KursEntity k")
public class KursEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id_kursu")
private long idKursu;

private BigDecimal cena;

@Temporal(TemporalType.DATE)
@Column(name="data_kursu")
private Date dataKursu;

//bi-directional many-to-one association to SpolkaEntity
@ManyToOne
@JoinColumn(name="nazwa_spolki")
private SpolkaEntity spolki;

public KursEntity() {
}

public KursEntity(long idKursu, BigDecimal cena, Date dataKursu, SpolkaEntity spolki) {
    this.idKursu = idKursu;
    this.cena = cena;
    this.dataKursu = dataKursu;
    this.spolki = spolki;
}

public long getIdKursu() {
    return this.idKursu;
}

public void setIdKursu(long idKursu) {
    this.idKursu = idKursu;
}

public BigDecimal getCena() {
    return this.cena;
}

public void setCena(BigDecimal cena) {
    this.cena = cena;
}

public Date getDataKursu() {
    return this.dataKursu;
}

public void setDataKursu(Date dataKursu) {
    this.dataKursu = dataKursu;
}

public SpolkaEntity getSpolki() {
    return this.spolki;
}

public void setSpolki(SpolkaEntity spolki) {
    this.spolki = spolki;
}

}

数据库context.xml中

 package pl.spring.demo.entity;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;

@Entity
@Table(name="spolki")
@NamedQuery(name="SpolkaEntity.findAll", query="SELECT s FROM SpolkaEntity s")
public class SpolkaEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private String nazwa;

//bi-directional many-to-one association to KursEntity
@OneToMany(mappedBy="spolki")
private List<KursEntity> kursy;

public SpolkaEntity() {
}

public SpolkaEntity(String nazwa, List<KursEntity> kursy) {
    this.nazwa = nazwa;
    this.kursy = kursy;
}

public String getNazwa() {
    return this.nazwa;
}

public void setNazwa(String nazwa) {
    this.nazwa = nazwa;
}

public List<KursEntity> getKursy() {
    return this.kursy;
}

public void setKursy(List<KursEntity> kursy) {
    this.kursy = kursy;
}

public KursEntity addKursy(KursEntity kursy) {
    getKursy().add(kursy);
    kursy.setSpolki(this);

    return kursy;
}

public KursEntity removeKursy(KursEntity kursy) {
    getKursy().remove(kursy);
    kursy.setSpolki(null);

    return kursy;
}

}

0 个答案:

没有答案