我只是不明白这一点。以下代码是怎么回事,错误在哪里?我必须上课:资源和预定。一个资源可以具有多个预留,并且该关系是双向的。对我来说,似乎无所不包,我已经看了一堆资源和文档-是的,还有很多例子,我无法弄清这一点的根本原因。
你们中的任何人都遇到了问题,或者至少有人可以告诉我,这没什么不对:)
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>
一个简单的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();
}
}
这是我得到的异常:
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
包含目标类的属性名称,该属性是关联的所有者。