Bonjour, je développe actuellement une appli web j2ee à l'aide de struts et hibernate. J'utilise éclipse, mysql et tomcat 5. J'arrive très bien à lire ce que ma bd contient, mais impossible de modifier ou de créer un enregistrement dans une table.
J'utilise un pattern DAO, dont voici le code :
Lorsque j'appelle la fonction listFilmDAO, il me trouve bien tous les enregistrements de la table. Par contre lorsque j'appelle insert sur un objet de type Film non null et non vide, il lance bien la requete SQL INSERT mais celle ci n'a aucun effet. Je vois qu'il lance la requete SQL car sous éclipse je vois les commandes SQL lancées à l'aide du plugin SYSDEO (enfin je crois que c grace a ce plugin).
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 public class HibernateFilmDAO implements FilmDAO { public void insertFilmDAO (Film film){ Session session = HibernateDAOFactory.creerConnexion(); try { session.save(film); session.flush(); } catch (HibernateException e) { e.printStackTrace(); } finally { try { HibernateUtil.closeSession(); } catch (HibernateException e1) { e1.printStackTrace(); } } } public List listFilmDAO () { Session session = HibernateDAOFactory.creerConnexion(); Query query = null; try { query = session.createQuery("from com.starm.hibernate.metier.Film film order by film.id"); List list = query.list(); Film film; for (int i = 0; i<list.size(); i++) { film = (Film) list.get(i); Hibernate.initialize( film.getFilmActeurs() ); Hibernate.initialize( film.getGenres() ); } return list; } catch (HibernateException e) { e.printStackTrace(); } finally { try { HibernateUtil.closeSession(); } catch (HibernateException e1) { e1.printStackTrace(); } } return null; } ..... }
Voici la commande qu'eclipse reconnait :
Je ne sais pas si la commande est normale (Les ? sont normaux ou est ce parce qu'il les considèers comme null ???)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Hibernate: insert into film (nom, datesortie, lien, chemin, fichier, extension, note, id) values (?, ?, ?, ?, ?, ?, ?, ?)
Bref je ne sais pas trop ce qui se passe réellement au niveau d'hibernate, mais en tout cas lorsque je veux lire un champ de l'objet Film que je passe en paramètre de la fonction InsertFilmDAO, je vois les champs rempli.
Pour vous aider, voici les conf d'hibernate :
Hibernate.cfg.xml :
Film.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
19
20
21
22 <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">starm</property> <property name="connection.password">******</property> <property name="connection.url">jdbc:mysql://localhost:3306/dvdtek</property> <property name="show_sql">true</property> <mapping resource="com/starm/hibernate/Acteur.hbm.xml" /> <mapping resource="com/starm/hibernate/Association.hbm.xml" /> <mapping resource="com/starm/hibernate/Film.hbm.xml" /> <mapping resource="com/starm/hibernate/FilmActeur.hbm.xml" /> <mapping resource="com/starm/hibernate/Genre.hbm.xml" /> <mapping resource="com/starm/hibernate/Player.hbm.xml" /> <mapping resource="com/starm/hibernate/User.hbm.xml" /> <mapping resource="com/starm/hibernate/UserPlayer.hbm.xml" /> </session-factory> </hibernate-configuration>
Si vous pouvez me fournir une explication de pourquoi ce fonctionne en lecture mais pas en écriture ce serait super.
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 <hibernate-mapping> <!-- Created by the Middlegen Hibernate plugin 2.2 http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/ --> <class name="com.starm.hibernate.metier.Film" table="film" lazy="false" > <id name="id" type="java.lang.Integer" column="id" > <generator class="increment" /> </id> <property name="nom" type="java.lang.String" column="nom" length="45" /> <property name="dateSortie" type="java.sql.Timestamp" column="datesortie" length="19" /> <property name="lien" type="java.lang.String" column="lien" length="80" /> <property name="chemin" type="java.lang.String" column="chemin" length="80" /> <property name="fichier" type="java.lang.String" column="fichier" length="45" /> <property name="extension" type="java.lang.String" column="extension" length="10" /> <property name="note" type="java.lang.Integer" column="note" length="10" /> <!-- Associations --> <!-- bi-directional one-to-many association to FilmActeur --> <set name="filmActeurs" lazy="true" inverse="true" cascade="all" > <key> <column name="idFilm" /> </key> <one-to-many class="com.starm.hibernate.metier.FilmActeur" /> </set> <!-- bi-directional one-to-many association to Genre --> <set name="genres" lazy="true" inverse="true" cascade="all" > <key> <column name="idFilm" /> </key> <one-to-many class="com.starm.hibernate.metier.Genre" /> </set> </class> </hibernate-mapping>
Merci d'avance.
Starm.
Partager