java JPA OneToMany ManyToOne @OneToOne 或 @ManyToOne 引用未知实体:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28865158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:18:38  来源:igfitidea点击:

JPA OneToMany ManyToOne @OneToOne or @ManyToOne on references an unknown entity:

javahibernatejpaannotations

提问by Ilir

I am just not getting the point here. Whats going on with the following code, where is the error? I have to Classes: Ressource and Reservation. A Resource can have multiple Reserverations and the relation is bidirectional. To me, eveything seems find and I have looked at a bunch ressources and documentations - ah yep, also at a lot of examples and I cant get to the rootcause of this.

我只是没有明白这一点。下面的代码是怎么回事,错误在哪里?我必须上课:资源和预订。一个资源可以有多个 Reserverations 并且关系是双向的。对我来说,一切似乎都找到了,我查看了一堆资源和文档-啊,是的,还有很多示例,但我无法找到其根本原因。

Any body of you gets the issue, or could somebody at least tell me that nothing is wrong with it:)

你们中的任何一个人都会遇到这个问题,或者至少有人可以告诉我它没有任何问题:)

package org.ademi.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Entity;

@Entity
@Table(name="Ressource")
public class Ressource implements Serializable{

    private static final long serialVersionUID = 12L;

    /**
     * A Ressource is available from a specific Date.
     */

    private Calendar availableFrom;

    /**
     * A Ressource is available until specific Date.
     */

    private Calendar availableTo;

    /**
     * This is a unique Ressource ID.
     */


    private int id;

    /**
     * A set of reservations that belong to this ressource
     */

    private List<Reservation> reservations;

    /**
     * A list of Days, when the ressource is available
     */


    private List<Day> daysAvailable;

    /**
     * This is specifying the intervall aloud for the reservation; 
     */

    private Intervall intervall;

    /**
     * Type of the ressource
     */

    private String type;

    /**
     * Name of the ressource
     */

    private String name;

    public Ressource(String name, String type, Calendar availableFrom, Calendar availableto, List<Day> daysAvailable, Intervall intervall){
        this.availableFrom = availableFrom;
        this.availableTo = availableto;
        this.daysAvailable = daysAvailable;
    }

    @Temporal(TemporalType.DATE)
    public Calendar getAvailableFrom() {
        return availableFrom;
    }

    public void setAvailableFrom(Calendar availableFrom) {
        this.availableFrom = availableFrom;
    }

    @Temporal(TemporalType.DATE)
    public Calendar getAvailableTo() {
        return availableTo;
    }

    public void setAvailableTo(Calendar availableTo) {
        this.availableTo = availableTo;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

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

    @OneToMany(mappedBy="Reservation",cascade = CascadeType.ALL)
    public List<Reservation> getReservations() {
        return reservations;
    }

    public void setReservations(List<Reservation> reservations) {
        this.reservations = reservations;
    }

    @Enumerated(EnumType.STRING)
    public Intervall getIntervall() {
        return intervall;
    }

    public void setIntervall(Intervall intervall) {
        this.intervall = intervall;
    }

    @ElementCollection(targetClass=Day.class)
    @Enumerated(EnumType.STRING)
    @CollectionTable(name="daysAvailable")
    @Column(name="daysAvailable")
    public List<Day> getDaysAvailable() {
        return daysAvailable;
    }

    public void setDaysAvailable(List<Day> daysAvailable) {
        this.daysAvailable = daysAvailable;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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



}


package org.ademi.model;

import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="Reservation")
public class Reservation implements Serializable{

    private static final long serialVersionUID = 1L;

    /**
     * A unique ID belonging to a reservation.
     */

    private int id;

    /**
     * Timesstamp for the beginning of the reservation
     */

    private Calendar reservationStarts;

    /**
     * Amount of Intervalls for the reservation 
     */

    private int reservedIntervalls;

    /**
     * A short summary Title describing the reservation
     */

    private String title;

    /**
     * The resource which is reserved in this reservation.
     */

    private Ressource ressource;

    public Reservation(String title, Ressource ressource, Calendar reservationStarts, int reservedIntervalls){
        this.title = title;
        this.ressource = ressource;
        this.reservationStarts = reservationStarts;
        this.reservedIntervalls = reservedIntervalls;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Calendar getReservationStarts() {
        return reservationStarts;
    }

    public void setReservationStarts(Calendar reservationStarts) {
        this.reservationStarts = reservationStarts;
    }

    public int getReservedIntervalls() {
        return reservedIntervalls;
    }

    public void setReservedIntervalls(int reservedIntervalls) {
        this.reservedIntervalls = reservedIntervalls;
    }

    @ManyToOne
    @JoinColumn(name="ressource_ID")
    public Ressource getRessource() {
        return ressource;
    }

    public void setRessource(Ressource ressource) {
        this.ressource = ressource;
    }


}

My persistence.xml

我的持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 
  xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
  http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">
  <persistence-unit name="iplandb">
    <properties>
      <!--
            <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            -->
      <property name="hibernate.archive.autodetection" value="class, hbm"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.connection.password" value="kbausbes"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/iplandb"/>
          <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.timeout" value="300"/>
            <property name="hibernate.c3p0.max_statements" value="50"/>
            <property name="hibernate.c3p0.idle_test_period" value="3000"/>
    </properties>
  </persistence-unit>
</persistence>

A simple TestingClass

一个简单的测试类

package org.ademi.client;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.ademi.model.Day;
import org.ademi.model.Intervall;
import org.ademi.model.Ressource;

public class TestClient {

    public static void main(String[] args){

        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("iplandb");

    /* Create EntityManager */
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();
    ArrayList<Day> a = new ArrayList<Day>();
    a.add(Day.FRIDAY);
    Ressource r = new Ressource("ilir", "ademi", new GregorianCalendar(), new GregorianCalendar(),a, Intervall.EIGHT );
    em.persist(r);
    em.flush();
    }
}

And this is the Exceptions I am getting:

这是我得到的例外:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: iplandb] Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    at org.ademi.client.TestClient.main(TestClient.java:20)
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.ademi.model.Reservation.ressource references an unknown entity: org.ademi.model.Ressource
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
    at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
    at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:438)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:309)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1162)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
    ... 4 more

回答by JB Nizet

The class is annotated with @org.hibernate.annotations.Entity. It must be annotated with javax.persistence.Entity.

该类用@org.hibernate.annotations.Entity. 必须用javax.persistence.Entity.

Fix your import.

修复您的导入。

Another problem is that mappedBy="Reservation"should be mappedBy="ressource". mappedBycontains the name of the property of the target class that is the owner side of the association.

另一个问题是mappedBy="Reservation"应该是mappedBy="ressource". mappedBy包含作为关联所有者端的目标类的属性名称。