Bonjour,
Depuis peu, je me suis mis à apprendre hibernate avec comme support, la doc officiel. Je suis arrivé à la gestion de l'heritage et j'ai voulu tester une des stratégie.
De ce fait j'ai développé un petit soft pour experimenter cela. Donc j'ai créer une classe personne et 2 classes (Indep & Salarie) qui hérite de celle-ci et pour pimenter ça j'ai créer 2 autres classes (email & tel) qui j'intégre au classes héritées comme components mais je rencontre une exception lorsque je veux exécuter l'application.
je sais que c'est farfelu mais mon but etait de mélanger divers points...Initial SessionFactory creation failed. org.hibernate.MappingException: invalid configuration
java.lang.NullPointerException
at hr.HrManager.createAndStoreSal(HrManager.java:49)
at hr.HrManager.main(HrManager.java:15)
java.lang.NullPointerException
at hr.HrManager.createAndStoreFree(HrManager.java:22)
at hr.HrManager.main(HrManager.java:16)
voici mon code :
voici son mappage :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package hr; public class Personne { public Personne(){} protected Long id; protected String name; protected String lastname; protected String addr; protected String year; protected Tel tel; protected Email email; ... les accesseurs ...
voici les classes héritées :
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 <?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 package="hr"> <class name="Personne" table="people"> <id name="id" column="id"> <generator class="native"/> </id> <property name="addr" /> <property name="name" /> <property name="lastname" /> <property name="year"/> <!-- les objets : Email, Tel--> <component name="email" class="Email"> <property name="eAddr" /> </component> <component name="tel" class="Tel"> <property name="country" /> <property name="number"/> <property name="prefix" /> </component> <joined-subclass name="Indep" table="Freelance"> <key column="id"/> <property name="company"/> </joined-subclass> <joined-subclass name="Salarie" table="Employe"> <key column="id"/> <property name="dept"/> <property name="fct"/> </joined-subclass> </class> </hibernate-mapping>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 package hr; public class Indep extends Personne { public Indep(){} private String company; ... accesseur ... }le reste :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 package hr; public class Salarie extends Personne{ public Salarie(){} private String dept; private String fct; ... accesseurs ... }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 package hr; public class Email { public Email(){} private String eAddr; ... accesseur }Ma classe hibernateUtil :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 package hr; public class Tel { public Tel(){} private String number; private String country; private String prefix; ... accesseurs ... }
Le fichier de config :
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 package util; import org.hibernate.*; import org.hibernate.cfg.*; public class HibernateUtil { private static SessionFactory sessionFactory; static{ try{ //create the SessionFactory from hibernate.cfg.xml //methode 1 : la plus courante sessionFactory=new Configuration().configure().buildSessionFactory(); //methode 2 /*sessionFactory=(SessionFactory) new Configuration() .setNamingStrategy(DefaultNamingStrategy.INSTANCE) .addFile("Event.hbm.xml") .addFile("Person.hbm.xml") .buildSessionFactory();*/ //methode 3 tourne //sessionFactory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); //methode 4 ne fonctionne pas pq ? Aucune idée mais je pense qu'il manque des paramètres //sessionFactory=(SessionFactory)new Configuration().addResource("events/Event.hbm.xml").addResource("events/Person.hbm.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 SessionFactory getSessionFactory(){ return sessionFactory; } }
et voici mon objet contenant mon main :
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
57
58
59
60 <?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> <!-- DB CONNECTION SETTINGS --> <property name="connection.driver_class">org.postgresql.Driver</property> <property name="connection.url">jdbc:postgresql://localhost/test</property> <property name="connection.username">Manu</property> <property name="connection.password">manu</property> <!-- JDBC connection pool --> <property name="connection.pool_size">1</property> <!-- SQL DIALECT --> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</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 DB SCHEMA ON STARTUP --> <property name="hbm2ddl.auto">create</property> <!-- MAPPING --> <!-- gestion evenements voir packages events--> <!--<mapping resource="events/Event.hbm.xml"/> <mapping resource="events/Person.hbm.xml"/>--> <!-- gestion piece auto voir packages cars --> <!--<mapping resource="cars/auto.hbm.xml"/> <mapping resource="cars/composant.hbm.xml"/>--> <!-- gestion HR voir pakages hr --> <maping resource="hr/Personne.hbm.xml"/> </session-factory> </hibernate-configuration>
Merci d'avance pour vos réactions et j'espère avoir fourni assez de données.
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
57
58
59
60
61
62
63
64
65
66
67
68 package hr; import org.hibernate.Session; import util.HibernateUtil; public class HrManager { public static void main(String[] args) { HrManager hrm=new HrManager(); Long idEmp=hrm.createAndStoreSal(); Long idFre=hrm.createAndStoreFree(); } public Long createAndStoreFree(){ Session session=null; Indep freel=new Indep(); try{ session=HibernateUtil.getSessionFactory().getCurrentSession(); System.out.println("SAISIE DONNEE FREEL : "); freel.setAddr("Av road drive 12A 90210 Beverly Hills"); freel.setCompany("SoftWear Factory"); Email box=new Email(); box.setEAddr("CEO@SoftWearFactory.us"); freel.setEmail(box); freel.setLastname("Lee"); freel.setName("Thampson"); Tel tel=new Tel(); tel.setCountry("USA"); tel.setNumber("08757575"); tel.setPrefix("dunno"); freel.setTel(tel); freel.setYear("47"); session.saveOrUpdate(freel); session.getTransaction().commit(); } catch(Exception e){ e.printStackTrace(); } return freel.getId(); } public Long createAndStoreSal(){ Session session=null; Salarie emp=new Salarie(); try{ session=HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); System.out.println("SAISIE DONNEE EMP : "); emp.setAddr("4rd Street 258 70658 Memphys"); emp.setDept("Accounting"); emp.setFct("Accounting Manager"); emp.setLastname("Teddy"); emp.setName("Cooper"); Email box=new Email(); box.setEAddr("totothediablo@company.us"); emp.setEmail(box); Tel tel=new Tel(); tel.setCountry("USA"); tel.setNumber("06858585"); tel.setPrefix("dunno"); emp.setTel(tel); emp.setYear("35"); session.saveOrUpdate(emp); session.getTransaction().commit(); } catch(Exception e){ e.printStackTrace(); } return emp.getId(); } }








Répondre avec citation
Partager