Bonjour à tous
Voici le problème auquel je suis confronté : j'ai une table qui décrit une Voiture, et dont la clé primaire est l'immatriculation. Il n'y a pas d'auto-increment ni de génération automatique sur ce champ.
J'utilise Eclipse 3.1, Hibernate 2, BD MySQL 4.1.9.
A l'exécution, j'obtiens l'erreur suivante :
Code:
INFO: Mapping resource: Voiture.hbm
19 sept. 2005 15:20:30 net.sf.hibernate.util.XMLHelper$ErrorLogger error
GRAVE: Error parsing XML: XML InputStream(10) Lélément "{0}" nécessite des éléments additionnels.
java.lang.ExceptionInInitializerError
at Test.main(Test.java:11)
Caused by: java.lang.RuntimeException: Problème de configuration : Error reading resource: Voiture.hbm
at com.gyom.hibernate.test.HibernateUtil.<clinit>(HibernateUtil.java:16)
... 1 more
Caused by: net.sf.hibernate.MappingException: Error reading resource: Voiture.hbm
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:339)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1013)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:969)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:883)
at com.gyom.hibernate.test.HibernateUtil.<clinit>(HibernateUtil.java:14)
... 1 more
Caused by: net.sf.hibernate.MappingException: invalid mapping
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
... 6 more
Caused by: org.xml.sax.SAXParseException: Lélément "{0}" nécessite des éléments additionnels.
at org.apache.crimson.parser.Parser2.error(Unknown Source)
at org.apache.crimson.parser.ValidatingParser$ChildrenValidator.done(Unknown Source)
at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
at org.apache.crimson.parser.Parser2.content(Unknown Source)
at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
at org.apache.crimson.parser.Parser2.content(Unknown Source)
at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
at org.apache.crimson.parser.Parser2.parse(Unknown Source)
at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:286)
... 7 more
Exception in thread "main"
Je vous fourni mes sources si cela peut vous aider à trouver...
Voitures
---------
Code:
CREATE TABLE `voitures` (
`marque` varchar(50) NOT NULL default '',
`modele` varchar(50) NOT NULL default '',
`moteur` varchar(50) NOT NULL default '',
`immatriculation` varchar(10) NOT NULL default '',
`proprietaire` int(11) NOT NULL default '0',
`couleur` varchar(50) NOT NULL default '',
PRIMARY KEY (`immatriculation`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Voiture.java
--------------
Code:
package com.gyom.hibernate.test;
public class Voiture
{
private String immatriculation;
private String marque;
private String modele;
private String moteur;
private Integer proprietaire;
private String couleur;
public Voiture()
{
}
public String getCouleur()
{
return couleur;
}
public void setCouleur(String couleur)
{
this.couleur = couleur;
}
public String getImmatriculation()
{
return immatriculation;
}
public void setImmatriculation(String immatriculation)
{
this.immatriculation = immatriculation;
}
public String getMarque()
{
return marque;
}
public void setMarque(String marque)
{
this.marque = marque;
}
public String getModele()
{
return modele;
}
public void setModele(String modele)
{
this.modele = modele;
}
public String getMoteur()
{
return moteur;
}
public void setMoteur(String moteur)
{
this.moteur = moteur;
}
public Integer getProprietaire()
{
return proprietaire;
}
public void setProprietaire(Integer proprietaire)
{
this.proprietaire = proprietaire;
}
}
Voiture.hbm
--------------
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.gyom.hibernate.test">
<class name="Voiture" table="voitures">
<id name="Immatriculation"
type="string"
column="immatriculation"
/>
<property
column="proprietaire"
length="11"
name="Proprietaire"
not-null="true"
type="integer"
/>
<property
column="moteur"
length="50"
name="Moteur"
not-null="true"
type="string"
/>
<property
column="marque"
length="50"
name="Marque"
not-null="true"
type="string"
/>
<property
column="couleur"
length="50"
name="Couleur"
not-null="true"
type="string"
/>
<property
column="modele"
length="50"
name="Modele"
not-null="true"
type="string"
/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
--------------------
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/projet_test
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password" />
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for MySQL -->
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.use_outer_join">true</property>
<!--
<property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
//-->
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="Event.hbm" />
<mapping resource="Employe.hbm" />
<mapping resource="Voiture.hbm" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
--------------------
Code:
package com.gyom.hibernate.test;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Crée la SessionFactory
sessionFactory =
new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de configuration : "
+ ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s!=null)
s.close();
}}
Test.java
----------
Code:
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import com.gyom.hibernate.test.HibernateUtil;
import com.gyom.hibernate.test.Voiture;
public class Test {
public static void main(String[] args) throws HibernateException{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Voiture v = new Voiture();
v.setImmatriculation("1000VB76");
v.setMarque("Peugeot");
v.setModele("406");
v.setMoteur("2.0L HDI 110ch");
v.setCouleur("Bordeaux");
v.setProprietaire(new Integer(2));
session.save(v);
tx.commit();
HibernateUtil.closeSession();
}
}
Si quelqu'un a une idée, elle est la bienvenue!