2016-12-25 73 views
0

我的實體有問題我把所有的註解,我也配置我的文件persistence.xml,以便它從實體自動生成一個表,但我總是在行中得到一個錯誤@Entity該表無法解析。表無法解析jpa

這裏是我的實體:

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實體:

package org.planetjpa; 

import java.io.Serializable; 
import java.util.ArrayList; 

import javax.persistence.*; 


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


@[email protected](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 { 


@[email protected](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; 
} 

} 
+1

請李宗紅實體的代碼添加到這個問題,或至少' Scences.liste_reserv_scenes'字段。錯誤消息說:'AnnotationException:非法嘗試將非集合映射爲@OneToMany,@ManyToMany或@CollectionOfElements:org.planetjpa.Scences.liste_reserv_scenes' - – krokodilko

+0

@krokodilko我已經做到了 – AmineBena17

+0

代碼對我來說看起來很好;字段的類型爲「java.util」Collection和一個實體(在persistence.xml中指定) - 可以將其視爲JPA提供程序中的錯誤/「功能」並報告。 FWIW無需指定依賴於實現的模式生成屬性,因爲JPA 2.1提供了這些標準化的 –

回答

0

貌似這個問題已經被問here。 集合的類型應該是一個接口,而不是一個實現。

Scenes.java從ArrayList的類型更改爲列表或任何類似的組,集合等集合接口的

@OneToMany(mappedBy = "scene_reserve") 
    private List<Reservationscene> liste_reserv_scenes = new ArrayList<>(); 
+0

如果您實際查看OP包含的最後一個類,實際上是java.util.List在該列表中。不管這是否是問題,那麼它需要被限定爲「JPA規範不會對用戶強加這一點,並且它只是一個需要這個的JPA提供者(Hibernate)。」;其他JPA提供者不會對其用戶進行任何這樣的強制代碼 –

+0

我將其更改爲ArrayList並使用JPA 2.1,但我仍然遇到同樣的問題 – AmineBena17

+0

嘗試在類型中使用List <>而不是ArrayList <>,看看是否解決了這個問題。 – vijayanand