Bonsoir
Je suis débutante en Hiebrnate et je trouve des difficulters j'espére que vous allez m'aide parceque vraiment je me bloque là
bon voila j'ai crée dans une premier temps une projet sous eclipse puis j'ai ejouter les libraires de hibernate + le driver de postgresql
aprés j'ai crée un fichier de configuration hibernate.cfg.xml voilà son contenu
aprés j'ai crée e fichier de mapping voilà son contenu
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 <?xml version="1.0" encoding="utf-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN" " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- local connection properties --> <property name="hibernate.connection.url"> jdbc:postgresql://localhost:5432/DBCRJJ </property> <property name="hibernate.connection.driver_class"> org.postgresql.Driver </property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password">1234</property> <!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for PostgreSQL --> <property name="dialect"> org.dialect.PostgreSQLDialect </property> <property name="hibernate.show_sql">false</property> <property name="hibernate.use_outer_join">true</property> <!-- <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property> <property name="jta.UserTransaction">java:comp/UserTransaction</property> //--> <property name="hibernate.transaction.factory_class"> org.hibernate.transaction.JDBCTransactionFactory </property> <mapping resource="contact.hbm" /> </session-factory> </hibernate-configuration>
j'ai crée une class bean voilà son contenu
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 <?xml version="1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN" " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="PkCOntact"> <class name="PKCOntact.contact" table="contact"> <id column="id" name="id" type="integer" > <generator class="increment" /> </id> <property column="prenom" length="25" name="prenom" not-null="false" type="string" /> <property column="age" length="10" name="age" not-null="false" type="integer" /> <property column="nom" length="25" name="nom" not-null="false" type="string" /> </class> </hibernate-mapping>
puis le class qui contient ma sessionfactory sous le nom de HibernateUtil.java voilà son contenu
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
48
49
50
51
52
53
54
55
56 package PkCOntact; public class contact { private int id; private String nom; private String prenom; private int age; public contact() { } public contact(int id, String nom, String prenom, int age) { this.id = id; this.nom = nom; this.prenom = prenom; this.age = age; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } }
aprés une class man MainRead.java voilà son contenu
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 package PkCOntact; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadlocal=new ThreadLocal<Session>(); private static final Configuration cfg =new Configuration(); private static org.hibernate.SessionFactory sessionFactory; public static Session currentSession()throws HibernateException{ Session session=(Session)threadlocal.get(); if(session==null||!session.isOpen()){ if(sessionFactory==null){ try{ cfg.configure(CONFIG_FILE_LOCATION); sessionFactory=cfg.buildSessionFactory(); } catch(Exception ex){ System.err.println("Erreur"); ex.printStackTrace(); }} session=(sessionFactory!=null)?sessionFactory.openSession():null; threadlocal.set(session);} return session; } public static void CloseSession()throws HibernateException{ Session session=(Session)threadlocal.get(); threadlocal.set(null); if(session!=null){ session.close(); } } }
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 package PkCOntact; import java.util.Iterator; import org.hibernate.Session; public class MainRead { public static void main(String []args){ Session session=HibernateUtil.currentSession(); Iterator iter=session.createQuery("from contact").iterate(); System.out.println("| id || nom || prenom || Age |"); while(iter.hasNext()){ contact contact=(contact)iter.next(); System.out.println("|"+contact.getId()); System.out.println(" "+contact.getNom()+" "); System.out.println(contact.getPrenom()+" "); System.out.println(contact.getAge()+" |"); System.out.println(); } HibernateUtil.CloseSession(); } }
j'ai executé j'ai reçu cette erreur
j'ai fait une recherche sur l'erreur "log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Erreur org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1500) at org.hibernate.cfg.Configuration.configure(Configuration.java:1434) at PkCOntact.HibernateUtil.currentSession(HibernateUtil.java:17) at PkCOntact.MainRead.main(MainRead.java:10) Caused by: org.dom4j.DocumentException: Error on line 2 of document : The markup in the document preceding the root element must be well-formed. Nested exception: The markup in the document preceding the root element must be well-formed. at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1490) ... 3 more Exception in thread "main" java.lang.NullPointerException at PkCOntact.MainRead.main(MainRead.java:11)
log4j:WARN Please initialize the log4j system properly." alors j'ai constaté qu'il faut que j' ajoute un fichier log4j.properties au src de mon projet avec se contenu
aprés que j'ai ajouté ce fichier log je ne recois plus l'erreur concernant le log
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 ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=debug, stdout log4j.logger.org.hibernate=info #log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### log4j.logger.org.hibernate.type=info ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=info ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### log cache activity ### log4j.logger.org.hibernate.cache=info ### log transaction activity #log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
je recois ces erreur
alors la je me bloque et j'attend votre aide
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 21:37:41,218 INFO Environment:514 - Hibernate 3.2.6 21:37:41,234 INFO Environment:547 - hibernate.properties not found 21:37:41,234 INFO Environment:681 - Bytecode provider name : cglib 21:37:41,234 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling 21:37:41,328 INFO Configuration:1432 - configuring from resource: /hibernate.cfg.xml 21:37:41,328 INFO Configuration:1409 - Configuration resource: /hibernate.cfg.xml 21:37:41,546 ERROR XMLHelper:61 - Error parsing XML: /hibernate.cfg.xml(2) The markup in the document preceding the root element must be well-formed. Erreur org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1500) at org.hibernate.cfg.Configuration.configure(Configuration.java:1434) at PkCOntact.HibernateUtil.currentSession(HibernateUtil.java:17) at PkCOntact.MainRead.main(MainRead.java:10) Caused by: org.dom4j.DocumentException: Error on line 2 of document : The markup in the document preceding the root element must be well-formed. Nested exception: The markup in the document preceding the root element must be well-formed. at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1490) ... 3 more Exception in thread "main" java.lang.NullPointerException at PkCOntact.MainRead.main(MainRead.java:11)
Merci d'avance
Partager