2017-06-27 3 views
-1

läuft mein erstes Projekt Hibernate Ich renne, die in MySQL-Datenbank Benutzerinformationen wie ID, Name und Adresse speichertIch erhalte org.hibernate.InvalidMappingException: Kann XML lesen, während einfache Hibernate Projekt

Dies ist mein Fehler

log4j:WARN No appenders could be found for logger (org.jboss.logging). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML 
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:490) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:486) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:659) 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:742) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2197) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2169) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2149) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2102) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2017) 
    at mypack.DataInsertion.insertInfo(DataInsertion.java:18) 
    at mypack.DataInsertion.main(DataInsertion.java:11) 
Caused by: org.dom4j.DocumentException: Error on line 3 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed. 
    at org.dom4j.io.SAXReader.read(SAXReader.java:482) 
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78) 
    ... 11 more 

hibernate.cfg.xml (xML-Konfigurationsdatei) log

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 

<!-- Related to the connection START --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/hibdb</property> 
    <property name="connection.user">root</property> 
    <property name="connection.password">admin</property> 
<!-- Related to the connection END -->  

<!-- Related to the hibernate properties START --> 
    <property name="show_sql">true</property> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<!-- Related to the hibernate properties END -->  

<!-- List of XML mapping files --> 
    <mapping resource="user.hbm.xml"/> 

</session-factory> 
</hibernate-configuration> 

user.hbm.xml (Mapping xml)

<!-- Mapping File to POJO Class --> 

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

<class name="mypack.DataProvider" table="user_info"> 
<id name="user_id" column="table_id"> 
<generator class="assigned"/> 
</id> 

<property name="user_name" column = "name" /> 
<property name="user_address" column = "address" /> 

</class> 

</hibernate-mapping> 

DataProvider.java (POJO Klasse)

// POJO Klasse

package mypack; 

public class DataProvider { 

private int user_id; 
private String user_name; 
private String user_address; 

public int getUser_id() { 
    return user_id; 
} 
public void setUser_id(int user_id) { 
    this.user_id = user_id; 
} 
public String getUser_name() { 
    return user_name; 
} 
public void setUser_name(String user_name) { 
    this.user_name = user_name; 
} 
public String getUser_address() { 
    return user_address; 
} 
public void setUser_address(String user_address) { 
    this.user_address = user_address; 
} 

} 

DataInsertion.java (implentation Java-Klasse)

package mypack; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

public class DataInsertion { 

    public static void main(String[] args) { 
     new DataInsertion().insertInfo(); 

    } 

    public void insertInfo() 
    { 
     Configuration con = new Configuration();  //interation with hib 
     con.configure("hibernate.cfg.xml");   //registering to xml 

     SessionFactory SF = con.buildSessionFactory(); //creating session 
     Session session = SF.openSession();   //opening new session 
     DataProvider provider = new DataProvider(); 
     provider.setUser_id(1); 
     provider.setUser_name("Goutham"); 
     provider.setUser_address("Hunsur"); 

     Transaction TR = session.beginTransaction(); 
     session.save(provider); 
     System.out.println("Object saved successfully"); 
     TR.commit();         //saving transaction 
     session.close(); 
     SF.close(); 
    } 

} 

Bitte beraten Sie mich ,,, Danke.

Antwort

0

Versuchen Sie, die Kommentarzeile und leere Zeile vor dem ersten Tag in user.hbm.xml

+0

Ich habe dieses Fehlerprotokoll nach der Korrektur dieses Problems zu entfernen: –

+0

log4j: WARN Kein Appen für Logger (org gefunden werden kann. jboss.logging). log4j: WARN Bitte initialisieren Sie das log4j-System ordnungsgemäß. Objekt erfolgreich gespeichert Hibernate: Einfügen in user_info (Name, Adresse, Tabelle_ID) Werte (?,?,?) Ausnahme im Thread "Haupt" org.hibernate.exception.SQLGrammarException: konnte Anweisung nicht ausführen –

+0

Sorry, ich habe das Recht output ,, in user_info einfügen (name, adresse, table_id), hier sollte table_id nur id sein. –

Verwandte Themen