Salut a tous ,
j'ai rencontré un problème lors de la sauvegarde d'un objet dans ma base de donnée.
voici le script de création de la base:
comme vous pouvez le voir historiqueUser a une clé primaire qui se compose du champ action et de la clé etrangere nomUser.
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 CREATE TABLE "utilisateurs" ( nom character(20) NOT NULL, prenom character(20) NOT NULL, CONSTRAINT nom PRIMARY KEY (nom) ) CREATE TABLE "historiqueUser" ( "nomUser" character(20) NOT NULL, "action" character(20) NOT NULL, CONSTRAINT "historiqueUser_pkey" PRIMARY KEY (action, "nomUser"), CONSTRAINT "historiqueUser_nomUser_fkey" FOREIGN KEY ("nomUser") REFERENCES utilisateurs (nom) MATCH SIMPLE )
Lors de la sauvegarde d'un objet HistoriqueUser j'ai les exceptions suivante
voila mes fichier de mapping
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: insert into historiqueUser (action, nomUser) values (?, ?) Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) at test.mainClass.main(mainClass.java:26) Caused by: java.sql.BatchUpdateException: L'élément du batch 0 insert into historiqueUser (action, nomUser) values (temp, momo) a été annulé. Appeler getNextException pour en connaître la cause. at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2531) at org.postgresql.core.v3.QueryExecutorImpl$1.handleError(QueryExecutorImpl.java:395) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1344) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:343) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2668) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ... 8 more
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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="packageHibernate"> <class name="Utilisateurs" table="utilisateurs" > <meta attribute="sync-DAO">false</meta> <meta attribute="sync-DAO">false</meta> <id name="name" type="string" column="nom" > </id> <property name="Prenom" column="prenom" type="string" not-null="true" length="20" /> </class> </hibernate-mapping>et mon .cfg
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"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="packageHibernate"> <class name="HistoriqueUser" table="historiqueUser" > <meta attribute="sync-DAO">false</meta> <composite-id> <key-property name="Action" column="action" type="string" /> <key-many-to-one name="NomUser" class="Utilisateurs" column="nomUser" /> </composite-id> </class> </hibernate-mapping>
et finalement le code qui pose probleme
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 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory > <!-- local connection properties --> <property name="hibernate.connection.url">jdbc:postgresql://localhost/baseChat</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password">motdepasse</property> <!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for PostgreSQL --> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <mapping resource="packageHibernate/Utilisateurs.hbm.xml"/> <mapping resource="packageHibernate/HistoriqueUser.hbm.xml"/> </session-factory> </hibernate-configuration>
Merci d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 Session mySession= HibernateUtil.currentSession(); Transaction tx =mySession.beginTransaction(); Utilisateurs u= (Utilisateurs) mySession.load(Utilisateurs.class,"momo"); HistoriqueUser hu=new HistoriqueUser(); hu.setNomUser(u); hu.setAction("temp"); mySession.save(hu); // exception declenché ici tx.commit(); mySession.close();
Partager