IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

problème d'insertion de données relation many to one


Sujet :

Hibernate Java

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 17
    Points
    17
    Par défaut problème d'insertion de données relation many to one
    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

  2. #2
    Membre averti Avatar de Shinzul
    Homme Profil pro
    Lecteur assidu de code source
    Inscrit en
    Janvier 2008
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Lecteur assidu de code source
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2008
    Messages : 174
    Points : 333
    Points
    333
    Par défaut
    Bonjour,
    En regardant l'erreur on voit que le problème se situe lors de l'ajout d'un employé, la colonne salle_fk ne peut pas etre null.
    Or tu save un employe qui ne possede pas de salle associé donc la constrainte not-null n'est pas respecté.

    Change ta contrainte en not-null="true" et regarde si ca marche
    N'oubliez pas le quand vous avez votre solution.

Discussions similaires

  1. Problème de persistance avec une relation many-to-one
    Par cb-06 dans le forum Doctrine2
    Réponses: 3
    Dernier message: 28/11/2012, 16h24
  2. Réponses: 0
    Dernier message: 12/02/2012, 17h02
  3. problème de jointure dans une relation many to one
    Par info_plus dans le forum JPA
    Réponses: 1
    Dernier message: 25/12/2008, 12h29
  4. problème d'insertion de données
    Par Falgan dans le forum ASP
    Réponses: 2
    Dernier message: 06/04/2004, 09h29
  5. [Interbase 7] Problème d'insertion de données
    Par Tuscelan dans le forum InterBase
    Réponses: 12
    Dernier message: 19/11/2003, 22h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo