应为元素类型“属性”声明属性“column”

时间:2016-10-01 13:06:07

标签: java xml hibernate

我正在尝试用Hibernate做一个简单的应用程序。 我创建了hibernate.cfg.xml和映射文件Personne.hbm.xml。 一旦我启动它,我得到一个异常:org.xml.sax.SAXParseException

详细说明: 我得到的错误是:

    13 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
15 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
19 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
24 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
75 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
75 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
152 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : bean/Personne.hbm.xml
177 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(12) L'attribut "column" doit être déclaré pour le type d'élément "properties".
177 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(13) L'attribut "column" doit être déclaré pour le type d'élément "properties".
178 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(14) L'attribut "column" doit être déclaré pour le type d'élément "properties".
178 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(15) L'attribut "column" doit être déclaré pour le type d'élément "properties".
Creation du sessionFactory a échouéorg.hibernate.InvalidMappingException: Could not parse mapping document from resource bean/Personne.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at util.HibernateUtil.<clinit>(HibernateUtil.java:16)
    at manager.PersonneManager.ajouterPersonne(PersonneManager.java:12)
    at test.TestMain.main(TestMain.java:15)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource bean/Personne.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:12)
    ... 2 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:549)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
    ... 9 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 52; L'attribut "column" doit être déclaré pour le type d'élément "properties".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1917)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:763)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:357)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2787)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
    ... 10 more

Personne.java

    package bean;

public class Personne {
    private int id;
    private String nom;
    private String prenom;
    private String tel;
    private String email;
    public Personne() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Personne(int id, String nom, String prenom, String tel, String email) {
        super();
        this.id = id;
        this.nom = nom;
        this.prenom = prenom;
        this.tel = tel;
        this.email = email;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNom() {
        return nom;
    }
    public void setNom(String nom) {
        this.nom = nom;
    }
    public String getPrenom() {
        return prenom;
    }
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "Personne [id=" + id + ", nom=" + nom + ", prenom=" + prenom
                + ", tel=" + tel + ", email=" + email + "]";
    }



}

Personne.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> 

    <class name="bean.Personne" table="PERSONNES"> 
        <id name="id" column="id_personne" > 
            <generator class="increment" /> 
        </id> 

        <properties name="nom" column="nom_personne" />
        <properties name="prenom" column="prenom_personne" />
        <properties name="tel" column="tel_personne" />
        <properties name="email" column="email_personne" />
    </class> 

</hibernate-mapping>

的hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <!-- a SessionFactory instance listed as /jndi/name -->
    <session-factory
        name="java:hibernate/SessionFactory">

        <!-- properties -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.uri">jdbc:mysql://localhost:3306/Gestion_BD_Personne</property>
        <property name="connection.username">Gestion_user</property>
        <property name="connection.password">user</property>

        <property name="connection.pool_size">1</property>

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="current_session_context_class">thread</property>

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">create</property>

        <!-- mapping files -->
        <mapping resource="bean/Personne.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

1 个答案:

答案 0 :(得分:1)

请将properties替换为此处property

<properties name="nom" column="nom_personne" />

需要更改为

<property name="nom" column="nom_personne" />