Bonjour,

Je me lance sur Hibernate !!! Tout se passe bien. J'ai quand même un problème assez important à résoudre.

J'utilise une base de données MySQL 5. J'ai 2 tables : Ressource et Niveau. J'ai une clé étrangère car, une ressource possède un et un seul niveau.

Lorsque je veux insérer un niveau tout se passe bien. Mais lorsque je veux ajouter une ressource ... POBLEME ...

Voici mon 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
23
24
25
26
27
28
29
 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="com.simecom.planning.pojo">
	<class name="Ressource" table="ressource">
		<id name="identifiant" column="ID_RES">
			<generator class="increment"/>
		</id>
		<property name="matricule" column="Matricule" />
		<property name="nom" column="NomR" />
		<property name="prenom" column="PrenomR" />
		<property name="motDePasse" column="MDPR" />
 
		<!--Champ de type Niveau-->
       <one-to-one name="leniveau" class="Niveau" cascade="none" constrained="true"></one-to-one>
	</class>
 
 
	<class name="Niveau" table="niveau">
		<id name="codeNiveau" column="CodeNiv">
			<generator class="increment"/>
		</id>		
		<property name="libelle" column="LibelleNiv" />		
	</class>
 
</hibernate-mapping>


Dans ma classe ressource j'ai cette propriété : private Niveau leniveau;

Le problème est lorsque j'essaie d'insérer une ressource suite à cette commande :

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
     Ressource res = new Ressource();
     res.setMatricule("?");
     res.setNom("?");
     res.setPrenom("?");
     res.setMotDePasse("?");
     res.setLeniveau(?);
     session.save(res);[/I]
 
(Les ? sont des données mais confidentielles ... )
 
 Le souci c'est que lorsque je fais le "save" en base, dans la table ressource, il attend l'identifiant et dans mon code, je lui passe un objet Niveau mais, je ne sais pas comment faire !!!!
 
 L'erreur renvoyée est :
 
[I]Hibernate: insert into planning.ressource (Matricule, NomR, PrenomR, MDPR, ID_RES) values (?, ?, ?, ?, ?)
15:44:10,977  WARN JDBCExceptionReporter:71 - SQL Error: 1452, SQLState: 23000
15:44:10,977 ERROR JDBCExceptionReporter:72 - Cannot add or update a child row: a foreign key constraint fails (`planning/ressource`, CONSTRAINT `FK_Ressource_Niveau` FOREIGN KEY (`ID_NIVEAU`) REFERENCES `niveau` (`CodeNiv`))
15:44:10,993 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
	at com.simecom.planning.test.Test.main(Test.java:70)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`planning/ressource`, CONSTRAINT `FK_Ressource_Niveau` FOREIGN KEY (`ID_NIVEAU`) REFERENCES `niveau` (`CodeNiv`))
	at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
	... 8 more