小编典典

java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey

hibernate

请帮我解决这个错误。按照下面的代码:

我的班级/实体人:

package entities;

import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Person {

@Id
@GeneratedValue
private long id;

@Column
private String name;

@Temporal(TemporalType.DATE)
private Calendar dateBirth;

@Column
private String email;

// MappedBy: Name of the instance that will indicate to whom One belongs, that is, an experience.
//           It should say which person is associated
// TargetEntity: Informs the entity we are associating
// FetchType.Lazy: Was chosen for performance
// Cascade: ALL to allow changes in all relationships.
 @OneToMany(mappedBy = "person", targetEntity = Experience.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
 private List<Experience> experiences;

// Getters and setters
public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Calendar getDateBirth() {
    return dateBirth;
}

public void setDateBirth(Calendar dateBirth) {
    this.dateBirth = dateBirth;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public List<Experience> getExperiences() {
    return experiences;
}

public void setExperiences(List<Experience> experiences) {
    this.experiences = experiences;
}

}

这是我的课程/实体体验:

package entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Experience {

@Id
@GeneratedValue
private long id;

@ManyToOne
@JoinColumn(name="idPerson", referencedColumnName="id")
private Person person;

@Column
private String company;

@Column
private String activities;

// Getters and setters

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}

public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getActivities() {
    return activities;
}

public void setActivities(String activities) {
    this.activities = activities;
}

}

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">

<persistence-unit name="professionalmanagementDB">

    <!-- JPA implementation -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <!-- Entities --> 
    <class>entities.Person</class>
    <class>entities.Experience</class>

    <properties> 
        <property name="hibernate.dialect"               value="org.hibernate.dialect.MySQLDialect" /> 
        <property name="hibernate.hbm2ddl.auto"          value="update" /> 
        <property name="hibernate.show_sql"              value="true" /> 
        <property name="hibernate.format_sql"            value="true" />  
        <property name="javax.persistence.jdbc.driver"   value="com.mysql.jdbc.Driver" /> 
        <property name="javax.persistence.jdbc.url"      value="jdbc:mysql://localhost/professionalmanagementDB" /> 
        <property name="javax.persistence.jdbc.user"     value="root" /> 
        <property name="javax.persistence.jdbc.password" value="" /> 
    </properties>

</persistence-unit>

</persistence>

因此,我正在尝试使用以下代码进行查询:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("professionalmanagementDB");
EntityManager manager = factory.createEntityManager();

String jpql = "select p ";
jpql += "from Person";

Query query = manager.createQuery(jpql);

List<Person> list = query.getResultList();

但总是会发生以下错误:

java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2881)
org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1795)
org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3790)
org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3744)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)
org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
dao.PersonDAO.readAll(PersonDAO.java:65)
manager.PersonManager.getPersons(PersonManager.java:17)
org.apache.jsp.HomeScreen_jsp._jspService(HomeScreen_jsp.java:87)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我的Maven依赖项如下:

javaee-api:7.0
hibernate-jpa-2.0-api:1.0.1.Final
hibernate-entitymanager:4.3.4.Final
mysql-connector-java:5.1.29

阅读 262

收藏
2020-06-20

共1个答案

小编典典

我不知道为什么Hibernate想要foreignKey@JoinColumn批注中使用该属性,但是它是在JPA
2.1中添加的
。使用JPA
2.1 API依赖性而不是2.0。

2020-06-20