gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:32:44 28 4
gpt4 key购买 nike

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

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

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;
}


}

我的persistence.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>

一个简单的测试类

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();
}
}

这是我得到的异常:

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

最佳答案

该类使用 @org.hibernate.annotations.Entity 注释。它必须用 javax.persistence.Entity 注释。

修复您的导入。

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

关于java - JPA OneToMany ManyToOne @OneToOne 或 @ManyToOne 引用未知实体 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865158/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com