我的实体有问题我把所有的注释都配置了我的文件persistence.xml,以便它从实体自动生成一个表但是我总是在行@Entity中得到一个错误,表不能得到解决。
这是我的实体:
package org.planetjpa;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
@Entity
public class Reservationscene implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="id_res_scene")
private Integer idResScene;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="date_reserv_scene")
private Date dateReservScene;
@ManyToOne
@JoinColumn(name="scene_id")
private Scences scene_reserve;
@ManyToOne
@JoinColumn(name="user_c_id")
private User user_courant;
public Scences getScene_reserve() {
return scene_reserve;
}
public void setScene_reserve(Scences scene_reserve) {
this.scene_reserve = scene_reserve;
}
public User getUser_courant() {
return user_courant;
}
public void setUser_courant(User user_courant) {
this.user_courant = user_courant;
}
public Reservationscene() {
}
public int getIdResScene() {
return this.idResScene;
}
public void setIdResScene(int idResScene) {
this.idResScene = idResScene;
}
public Date getDateReservScene() {
return this.dateReservScene;
}
public void setDateReservScene(Date dateReservScene) {
this.dateReservScene = dateReservScene;
}
}
Scences Entity:
package org.planetjpa;
import java.io.Serializable;
import java.util.ArrayList;
import javax.persistence.*;
@Entity
@Table(name = "scencesnational")
public class Scences implements Serializable {
@Id@Column(name="Id")
private int id_scene;
@Column(name="NOM")
private String nom_scenes;
@Column(name="Addresse")
private String adresse_scenes;
@Column(name = "`ADRESSE 2`")
private String boite_postale;
@Column(name="CP")
private String code_postale;
@Column(name="Surface")
private double surface_scence;
@Column(name="Type")
private String type_scene;
@Column(name="Capacité")
private int capacite_scene;
@Column(name="`Prix réservation`")
private double prix_scene;
private static final long serialVersionUID = 1L;
@OneToMany(mappedBy="scene_reserve")
private ArrayList<Reservationscene> liste_reserv_scenes;
public Scences() {
super();
}
public int getId_scene() {
return id_scene;
}
public void setId_scene(int id_scene) {
this.id_scene = id_scene;
}
public String getNom_scenes() {
return nom_scenes;
}
public void setNom_scenes(String nom_scenes) {
this.nom_scenes = nom_scenes;
}
public String getAdresse_scenes() {
return adresse_scenes;
}
public void setAdresse_scenes(String adresse_scenes) {
this.adresse_scenes = adresse_scenes;
}
public String getBoite_postale() {
return boite_postale;
}
public void setBoite_postale(String boite_postale) {
this.boite_postale = boite_postale;
}
public String getCode_postale() {
return code_postale;
}
public void setCode_postale(String code_postale) {
this.code_postale = code_postale;
}
public double getSurface_scence() {
return surface_scence;
}
public void setSurface_scence(double surface_scence) {
this.surface_scence = surface_scence;
}
public String getType_scene() {
return type_scene;
}
public void setType_scene(String type_scene) {
this.type_scene = type_scene;
}
public int getCapacite_scene() {
return capacite_scene;
}
public void setCapacite_scene(int capacite_scene) {
this.capacite_scene = capacite_scene;
}
public double getPrix_scene() {
return prix_scene;
}
public void setPrix_scene(double prix_scene) {
this.prix_scene = prix_scene;
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="projetjsf">
<class>org.planetjpa.Films</class>
<class>org.planetjpa.Lieux</class>
<class>org.planetjpa.Reservationscene</class>
<class>org.planetjpa.Scences</class>
<class>org.planetjpa.User</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/projetjsf"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="amine1993"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
当我尝试在JPA工具中从实体生成表时,我得到了这个错误:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: projetjsf] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.buildEntityManagerFactory(Main.java:94)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:80)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:68)
Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: org.planetjpa.Scences.liste_reserv_scenes
at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:321)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1693)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:765)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:684)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3406)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3360)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1334)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
... 6 more
Scenes.java:
package org.planetjpa;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Scences
*
*/
@Entity
@Table(name = "scencesnational")
public class Scences implements Serializable {
@Id@Column(name="Id")
private int id_scene;
@Column(name="NOM")
private String nom_scenes;
@Column(name="Addresse")
private String adresse_scenes;
@Column(name = "`ADRESSE 2`")
private String boite_postale;
@Column(name="CP")
private String code_postale;
@Column(name="Surface")
private double surface_scence;
@Column(name="Type")
private String type_scene;
@Column(name="Capacité")
private int capacite_scene;
@Column(name="`Prix réservation`")
private double prix_scene;
private static final long serialVersionUID = 1L;
@OneToMany(mappedBy="scene_reserve")
private List<Reservationscene> liste_reserv_scenes;
public Scences() {
super();
}
public int getId_scene() {
return id_scene;
}
public void setId_scene(int id_scene) {
this.id_scene = id_scene;
}
public String getNom_scenes() {
return nom_scenes;
}
public void setNom_scenes(String nom_scenes) {
this.nom_scenes = nom_scenes;
}
public String getAdresse_scenes() {
return adresse_scenes;
}
public void setAdresse_scenes(String adresse_scenes) {
this.adresse_scenes = adresse_scenes;
}
public String getBoite_postale() {
return boite_postale;
}
public void setBoite_postale(String boite_postale) {
this.boite_postale = boite_postale;
}
public String getCode_postale() {
return code_postale;
}
public void setCode_postale(String code_postale) {
this.code_postale = code_postale;
}
public double getSurface_scence() {
return surface_scence;
}
public void setSurface_scence(double surface_scence) {
this.surface_scence = surface_scence;
}
public String getType_scene() {
return type_scene;
}
public void setType_scene(String type_scene) {
this.type_scene = type_scene;
}
public int getCapacite_scene() {
return capacite_scene;
}
public void setCapacite_scene(int capacite_scene) {
this.capacite_scene = capacite_scene;
}
public double getPrix_scene() {
return prix_scene;
}
public void setPrix_scene(double prix_scene) {
this.prix_scene = prix_scene;
}
}
答案 0 :(得分:0)
看起来这个问题已被问到here。 集合的类型应该是接口,而不是实现。
在 Scenes.java 中,将类型从ArrayList更改为List或任何集合接口,如Set,Collection等。
@OneToMany(mappedBy = "scene_reserve")
private List<Reservationscene> liste_reserv_scenes = new ArrayList<>();