salut a tous,
j'ai une erreur et je crois que la raison soit le fichier de configuration hibernate.cfg.xml soit du à un pb de connexion à la base Mysql (je travail sous eclipse Tomcat6).
je présente le code de cette petite application:
classe emp.java
et le fichier de mapping emp.hbm.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package first; public class Emp { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
et voici le fichier de configuration hibernate.cfg.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 <?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="Emp" table="EMPLOYEE"> <id name="id" column="id" type="long"> <generator class="increment"/> // This generates the primary key </id> <property name="name" column="name"/> </class> </hibernate-mapping>
et finalement la classe de teste TestExemple.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 <?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-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/amed</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="hibernate.c3p0.min_size">1</property> <property name="hibernate.c3p0.max_size">4</property> <property name="hibernate.c3p0.timeout">1800</property> <property name="hibernate.c3p0.max_statements">50</property> <!-- MySQL dialect//different for different Database --> <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> <property name="hbm2ddl.auto">update</property> <mapping resource="Emp.hbm.xml"/> </session-factory> </hibernate-configuration>
et le message d'erreur est le suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import first.Emp; public class TestExample { public static void main(String[] args) { Session session = null; try{ // This step will read hibernate.cfg.xml SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); System.out.println("Inserting Records"); Emp emp1 = new Emp(); emp1.setName("Nick"); session.save(emp1); Emp emp2 = new Emp(); emp2.setName("Das"); session.save(emp2); session.flush(); // insert data into databse System.out.println("Save Done- finish database insert"); // Now fetch the Employee data List empList = session.createQuery("from Emp").list(); //empList contains the list of Employee for(int i=0;i<8;i++) { Emp emp = (Emp)empList.get(i); System.out.println(emp.getName()); } // Out put is : Nick Das }catch(Exception e){ }finally{ session.close(); } } }
merci d'avance....
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 11 févr. 2009 14:16:39 org.hibernate.cfg.Environment <clinit> INFO: Hibernate 3.2.3 11 févr. 2009 14:16:39 org.hibernate.cfg.Environment <clinit> INFO: hibernate.properties not found 11 févr. 2009 14:16:39 org.hibernate.cfg.Environment buildBytecodeProvider INFO: Bytecode provider name : cglib 11 févr. 2009 14:16:39 org.hibernate.cfg.Environment <clinit> INFO: using JDK 1.4 java.sql.Timestamp handling 11 févr. 2009 14:16:40 org.hibernate.cfg.Configuration configure INFO: configuring from resource: /hibernate.cfg.xml 11 févr. 2009 14:16:40 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: Configuration resource: /hibernate.cfg.xml 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(5) Document root element "hibernate-configuration", must match DOCTYPE root "hibernate-mapping". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(5) Element type "hibernate-configuration" must be declared. 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(6) Element type "session-factory" must be declared. 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(8) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(9) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(10) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(13) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(14) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(15) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(16) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(18) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(20) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(22) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(24) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(25) The content of element type "property" must match "(meta*,(column|formula)*,type?)". 11 févr. 2009 14:16:40 org.hibernate.util.XMLHelper$ErrorLogger error GRAVE: Error parsing XML: /hibernate.cfg.xml(26) Element type "mapping" must be declared. Exception in thread "main" java.lang.NullPointerException at TestExample.main(TestExample.java:35)
Partager