0
我是hibernate編程的初學者,所以我試圖運行我的第一個項目hibernate,但它不能在任何時間運行我運行它 這對SOURSE代碼項目從* .hbm.xml和* .cfg.xml本地化到運行所有項目的Hibernate
此捕獲project exlorer和this is the complement project explorer是源代碼:
Categorie.java
package dao;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Categorie {
private Long idCategorie;
private String titre, description;
private Date date;
private Set<Produit> produits = new HashSet();
public Categorie() {
super();
}
public Categorie(String titre, String description, Date date) {
super();
this.titre = titre;
this.description = description;
this.date = date;
}
public Long getIdCategorie() {
return idCategorie;
}
public void setIdCategorie(Long idCategorie) {
this.idCategorie = idCategorie;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
Produit.java
package dao;
public class Produit {
private Long idProduit;
private String nom, description;
private Double prix;
public Produit() {
super();
}
public Produit(String nom, String description, Double prix) {
super();
this.nom = nom;
this.description = description;
this.prix = prix;
}
public Long getIdProduit() {
return idProduit;
}
public void setIdProduit(Long idProduit) {
this.idProduit = idProduit;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrix() {
return prix;
}
public void setPrix(Double prix) {
this.prix = prix;
}
}
Service.java
package dao;
import org.hibernate.Session;
import util.HibernateUtil;
public class Services {
public void addProduit(Produit p){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
}
}
test.java
package dao;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Services s =new Services();
Produit p = new Produit("PC", "Sony viao", (double)7000);
s.addProduit(p);
}
}
HibernateUtil.java
package util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Création de la SessionFactory à partir de hibernate.cfg.xml
sessionFactory = new Configuration().configure("Hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
// public static final ThreadLocal session = new ThreadLocal();
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
就從這裏它的Java代碼,從現在它是個XML:
Categorie.hbm.xml
<?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="dao.Categorie" table="CATEGORIE">
<id name="idCategorie" column="ID_CATEGORIE">
<generator class="native"></generator>
</id>
<nom name="titre" column="TITRE"></nom>
<description name="description" column="DESCRIPTION"></description>
<prix name="date" column="DATE"></prix>
<set name="produit" table="CAT_PROD">
<key column="ID_CATEGORIE" ></key>
<many-to-many class="dao.Produit" column="ID_PRODUIT"></many-to-many>
</set>
</class>
</hibernate-mapping>
Produit.hbm.xml
<?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="dao.Produit" table="PRODUITS">
<id name="idProduit" column="ID_PRODUIT">
<generator class="native" ></generator>
</id>
<nom name="nom" column="NOM"></nom>
<description name="description" column="DESCRIPTION"></description>
<prix name="prix" column="PRIX"></prix>
</class>
</hibernate-mapping>
的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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="dao/Categorie.hbm.xml" />
<mapping resource="dao/Produit.hbm.xml" />
</session-factory>
</hibernate-configuration>
這是從控制檯erreurs
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not read mappings from resource: dao/Categorie.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(HibernateUtil.java:17)
at dao.Services.addProduit(Services.java:8)
at dao.Test.main(Test.java:9)
Caused by: org.hibernate.MappingException: Could not read mappings from resource: dao/Categorie.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:484)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1443)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1411)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1392)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1368)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1288)
at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
... 2 more
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:424)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:481)
... 8 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 36; Le type d'élément "nom" doit être déclaré.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:421)
... 9 more
所以請幫忙展示我清楚地步步 和非常感謝