0

我正在尝试将 Hibernate 与 GWT 一起使用,但出现此错误:

org.hibernate.MappingException: Unknown entity: com.asso.shared.Personne

我不明白为什么我得到这个错误!我正在使用注释,这些类是由 Hibernate 生成的!Personne 类:

package com.asso.shared;

// Generated 26 mai 2013 23:54:30 by Hibernate Tools 3.4.0.CR1

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Personne generated by hbm2java
 */
@Entity
@Table(name = "personne", catalog = "mydb")
public class Personne implements java.io.Serializable {

    private PersonneId id;
    private String nom;
    private String prenom;
    private String photo;
    private Date dnaissance;
    private String civilite;
    private Boolean sexe;
    private String email;
    private String site;
    private String telPortable;
    private String telDomicile;
    private String telBureau;
    private String adresse;
    private String ville;
    private String region;
    private String cpostal;
    private String pays;
    private Date dadhesion;
    private Double montantContisation;
    private Double mpaye;
    private Date dpayement;
    private String observations;
    private String mdp;
    private String titre;
    private Set<Doc> docs = new HashSet<Doc>(0);
    private Set<Droits> droitses = new HashSet<Droits>(0);
    private Set<Activite> activites = new HashSet<Activite>(0);
    private Set<Groupe> groupes = new HashSet<Groupe>(0);
    private Set<Courrier> courriers = new HashSet<Courrier>(0);

    public Personne() {
    }

    public Personne(PersonneId id) {
        this.id = id;
    }

    public Personne(PersonneId id, String nom, String prenom, String photo,
            Date dnaissance, String civilite, Boolean sexe, String email,
            String site, String telPortable, String telDomicile,
            String telBureau, String adresse, String ville, String region,
            String cpostal, String pays, Date dadhesion,
            Double montantContisation, Double mpaye, Date dpayement,
            String observations, String mdp, String titre, Set<Doc> docs,
            Set<Droits> droitses, Set<Activite> activites, Set<Groupe> groupes,
            Set<Courrier> courriers) {
        this.id = id;
        this.nom = nom;
        this.prenom = prenom;
        this.photo = photo;
        this.dnaissance = dnaissance;
        this.civilite = civilite;
        this.sexe = sexe;
        this.email = email;
        this.site = site;
        this.telPortable = telPortable;
        this.telDomicile = telDomicile;
        this.telBureau = telBureau;
        this.adresse = adresse;
        this.ville = ville;
        this.region = region;
        this.cpostal = cpostal;
        this.pays = pays;
        this.dadhesion = dadhesion;
        this.montantContisation = montantContisation;
        this.mpaye = mpaye;
        this.dpayement = dpayement;
        this.observations = observations;
        this.mdp = mdp;
        this.titre = titre;
        this.docs = docs;
        this.droitses = droitses;
        this.activites = activites;
        this.groupes = groupes;
        this.courriers = courriers;
    }

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "cin", column = @Column(name = "CIN", nullable = false)),
            @AttributeOverride(name = "num", column = @Column(name = "Num", nullable = false)) })
    public PersonneId getId() {
        return this.id;
    }

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

    @Column(name = "Nom", length = 45)
    public String getNom() {
        return this.nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @Column(name = "Prenom", length = 45)
    public String getPrenom() {
        return this.prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    @Column(name = "Photo", length = 70)
    public String getPhoto() {
        return this.photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "DNaissance", length = 10)
    public Date getDnaissance() {
        return this.dnaissance;
    }

    public void setDnaissance(Date dnaissance) {
        this.dnaissance = dnaissance;
    }

    @Column(name = "Civilite", length = 5)
    public String getCivilite() {
        return this.civilite;
    }

    public void setCivilite(String civilite) {
        this.civilite = civilite;
    }

    @Column(name = "Sexe")
    public Boolean getSexe() {
        return this.sexe;
    }

    public void setSexe(Boolean sexe) {
        this.sexe = sexe;
    }

    @Column(name = "Email", length = 45)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "Site", length = 45)
    public String getSite() {
        return this.site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    @Column(name = "TelPortable", length = 45)
    public String getTelPortable() {
        return this.telPortable;
    }

    public void setTelPortable(String telPortable) {
        this.telPortable = telPortable;
    }

    @Column(name = "TelDomicile", length = 45)
    public String getTelDomicile() {
        return this.telDomicile;
    }

    public void setTelDomicile(String telDomicile) {
        this.telDomicile = telDomicile;
    }

    @Column(name = "TelBureau", length = 45)
    public String getTelBureau() {
        return this.telBureau;
    }

    public void setTelBureau(String telBureau) {
        this.telBureau = telBureau;
    }

    @Column(name = "Adresse", length = 45)
    public String getAdresse() {
        return this.adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    @Column(name = "Ville", length = 45)
    public String getVille() {
        return this.ville;
    }

    public void setVille(String ville) {
        this.ville = ville;
    }

    @Column(name = "Region", length = 45)
    public String getRegion() {
        return this.region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    @Column(name = "CPostal", length = 45)
    public String getCpostal() {
        return this.cpostal;
    }

    public void setCpostal(String cpostal) {
        this.cpostal = cpostal;
    }

    @Column(name = "Pays", length = 45)
    public String getPays() {
        return this.pays;
    }

    public void setPays(String pays) {
        this.pays = pays;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "DAdhesion", length = 10)
    public Date getDadhesion() {
        return this.dadhesion;
    }

    public void setDadhesion(Date dadhesion) {
        this.dadhesion = dadhesion;
    }

    @Column(name = "MontantContisation", precision = 22, scale = 0)
    public Double getMontantContisation() {
        return this.montantContisation;
    }

    public void setMontantContisation(Double montantContisation) {
        this.montantContisation = montantContisation;
    }

    @Column(name = "MPaye", precision = 22, scale = 0)
    public Double getMpaye() {
        return this.mpaye;
    }

    public void setMpaye(Double mpaye) {
        this.mpaye = mpaye;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "DPayement", length = 10)
    public Date getDpayement() {
        return this.dpayement;
    }

    public void setDpayement(Date dpayement) {
        this.dpayement = dpayement;
    }

    @Column(name = "Observations")
    public String getObservations() {
        return this.observations;
    }

    public void setObservations(String observations) {
        this.observations = observations;
    }

    @Column(name = "MDP")
    public String getMdp() {
        return this.mdp;
    }

    public void setMdp(String mdp) {
        this.mdp = mdp;
    }

    @Column(name = "Titre", length = 45)
    public String getTitre() {
        return this.titre;
    }

    public void setTitre(String titre) {
        this.titre = titre;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "personne")
    public Set<Doc> getDocs() {
        return this.docs;
    }

    public void setDocs(Set<Doc> docs) {
        this.docs = docs;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "personne_has_droits", catalog = "mydb", joinColumns = {
            @JoinColumn(name = "Personne_CIN", nullable = false, updatable = false),
            @JoinColumn(name = "Personne_Num", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "Droits_Id", nullable = false, updatable = false) })
    public Set<Droits> getDroitses() {
        return this.droitses;
    }

    public void setDroitses(Set<Droits> droitses) {
        this.droitses = droitses;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "activite_has_personne", catalog = "mydb", joinColumns = {
            @JoinColumn(name = "Personne_CIN", nullable = false, updatable = false),
            @JoinColumn(name = "Personne_Num", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "Activite_idActivite", nullable = false, updatable = false) })
    public Set<Activite> getActivites() {
        return this.activites;
    }

    public void setActivites(Set<Activite> activites) {
        this.activites = activites;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "groupe_has_personne", catalog = "mydb", joinColumns = {
            @JoinColumn(name = "Personne_CIN", nullable = false, updatable = false),
            @JoinColumn(name = "Personne_Num", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "Groupe_idGroupe", nullable = false, updatable = false) })
    public Set<Groupe> getGroupes() {
        return this.groupes;
    }

    public void setGroupes(Set<Groupe> groupes) {
        this.groupes = groupes;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "personne")
    public Set<Courrier> getCourriers() {
        return this.courriers;
    }

    public void setCourriers(Set<Courrier> courriers) {
        this.courriers = courriers;
    }

}

我用来持久化 Personne 类的方法:

public int ajouterAdherent(Personne p) {
        Session session = HibernateUtil.currentSession();
        Transaction tx = null;
        System.out.println("insertion");
        try {
            tx = session.beginTransaction();
            p.toString();
            session.save(p);
            tx.commit();

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
                HibernateUtil.closeSession();
                e.printStackTrace();
            }
            HibernateUtil.closeSession();
            return -1;
        }
        return 1;
    }

这是hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
    </session-factory>
</hibernate-configuration>
4

1 回答 1

1

这个问题与 GWT 无关。

你得到: org.hibernate.MappingException 因为 Hibernate 不知道你的实体类。你必须添加

<mapping class="com.asso.shared.Personne"/>

在 hbm.xml 文件中,或创建 SessionFactory

sessionFactory = new AnnotationConfiguration()
                   .addAnnotatedClass(...)

阅读http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html

于 2013-05-27T23:27:01.083 回答