a tous;
je suis débutant en hibernate et mon problème est le suivant:
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
 
1125 [main] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 1048, SQLState: 23000
1125 [main] ERROR org.hibernate.util.JDBCExceptionReporter  - null,  message from server: "Column 'salle_fk' cannot be null"
1125 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - 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:253)
	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
	at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2224)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
	at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
	at test.HibernateTest.main(HibernateTest.java:79)
Caused by: java.sql.BatchUpdateException: null,  message from server: "Column 'salle_fk' cannot be null"
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1404)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
	... 15 more
Exception in thread "main" 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:253)
	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
	at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2224)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
	at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
	at test.HibernateTest.main(HibernateTest.java:79)
Caused by: java.sql.BatchUpdateException: null,  message from server: "Column 'salle_fk' cannot be null"
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1404)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
	... 15 more
en faite l'application il y a seulement deux classes Employee et Salle donc on a une relation 1-n
voici le fichier de mapping Employee.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
 
<?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="dto">
	<class name="Employee" table="employee">
		<id name="idEmployee" column="employee_id"
			type="java.lang.Integer">
			<generator class="increment" />
		</id>
		<property name="nom" column="nom" type="java.lang.String" />
		<property name="prenom" column="prenom" type="java.lang.String" />
		<property name="login" column="login" type="java.lang.String" />
		<property name="pwd" column="pwd" type="java.lang.String" />
		<property name="privilege" column="privilege" type="java.lang.Boolean" />
		<property name="email" column="email" type="java.lang.String" />
 
		<many-to-one name="salle" column="salle_fk" not-null="false"/>     	
 
	</class>
</hibernate-mapping>
et voici le fichier Salle.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
 
<?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="dto">
	<class name="Salle" table="salle">
		<id name="idSalle" column="salle_id" type="java.lang.Integer">
			<generator class="increment" />
		</id>
		<property name="nomSalle" column="nom_salle" type="java.lang.String" />
		<property name="lieu" column="lieu" type="java.lang.String" />
		<property name="nbrePlace" column="nbre_place" type="java.lang.Integer" />
 
		<set name="employees" inverse="false" >
			<key>
				<column name="salle_fk"></column>
			</key>
			<one-to-many class="Employee" />
		</set>
	</class>
</hibernate-mapping>
et voici le fichier de HbernateTest.java
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
 
	public static void main(String[] args) {
 
			session = HibernateSessionFactory.currentSession();
			System.out.println("Adding and removing relations");
			Transaction tx = session.beginTransaction();
			dto.Employee emp = new dto.Employee();
			emp.setNom("BENC");
			emp.setPrenom("Amed");
			emp.setLogin("amed");
			emp.setPrivilege(false);
			emp.setPwd("amed1");
			emp.setEmail("ahmed@hr.com");
			session.save(emp);
			System.out.println("### EMPPPP ######");
			dto.Employee emp1 = new dto.Employee();
			emp1.setNom("BENC");
			emp1.setPrenom("Aymen");
			emp1.setLogin("aymen");
			emp1.setPrivilege(false);
			emp1.setPwd("aymen1");
			emp1.setEmail("aymed@hr.com");
			session.save(emp1);
			System.out.println("#### EMP1 #####");		
			Salle sal=new Salle();
			sal.setIdSalle(3);
			sal.setLieu("HR access");
			sal.setNbrePlace(33);
			sal.setNomSalle("Salle 3");
			session.save(sal);
			System.out.println("#### SALLE #####");	
			sal.getEmployees().add(emp1);
			sal.getEmployees().add(emp);
			session.save(sal);
			System.out.println(sal.toString());
			System.out.println("*********************");	
 
			tx.commit();
	}
}
et merci d'avance.
BEST REGARD