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 :

pb d'insertion dans une table lier a d'autres


Sujet :

Hibernate Java

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 47
    Points : 20
    Points
    20
    Par défaut pb d'insertion dans une table lier a d'autres
    bonjour,
    come je suis debutant en hibernate j'arrive pas à surmonter le pb suivant:
    j'ai une table (bon sortie) qui est liée à d'autre table et lorsque je teste l'insertion dans cette table par la classe suivante:
    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
     
     
    import org.hibernate.*;
     
    import test.abc;
    import util.HibernateUtil;
    import data.BonSortie;
    import data.Materiel;
    import data.Societe;
    import data.TypeMateriel;
     
    public class testinsert {
     
    	 public static void main(String[] args)
    		throws HibernateException {
     
    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		 Transaction tx=session.beginTransaction();
     
    		 BonSortie d1 = new BonSortie();
     
    		 d1.setId_sortie(1234);
    		 d1.setId_societe(1111);
    		 d1.setN_bon("bonjour");
    		 d1.setDate_s("1/1/2008");
    		session.save(d1);  
     
     
     
    		 tx.commit();
    		 HibernateUtil.getSessionFactory().close();
    		 System.out.println("bonjour");
    	 }
    }
    on me donne toujours l'erreur suivante:
    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
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
     
    Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of data.Societe.id_societe
    	at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
    	at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
    	at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3524)
    	at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3240)
    	at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
    	at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
    	at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
    	at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
    	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
    	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
    	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
    	at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
    	at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    	at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    	at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    	at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
    	at $Proxy0.save(Unknown Source)
    	at testinsert.main(testinsert.java:35)
    Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
    	... 25 more
    alors que le pojo correspondant a cete table est:

    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
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    package data;
     
    import java.util.HashSet;
    import java.util.Set;
     
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.validator.ValidatorForm;
     
     
    public class BonSortie extends ValidatorForm{
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	private long id_sortie;
     
    	private long id_mat;
     
    	private long id_societe;
     
    	private long id_interv_soc;
     
    	private String n_bon;
     
    	private String date_s;
     
    	private String objet;
     
    	private String obs_s;
     
    	private Set intervention_societes = new HashSet();
     
    	public static long getSerialVersionUID() {
    		return serialVersionUID;
    	}
     
    	public String getDate_s() {
    		return date_s;
    	}
     
    	public void setDate_s(String date_s) {
    		this.date_s = date_s;
    	}
     
    	public long getId_interv_soc() {
    		return id_interv_soc;
    	}
     
    	public void setId_interv_soc(long id_interv_soc) {
    		this.id_interv_soc = id_interv_soc;
    	}
     
    	public long getId_mat() {
    		return id_mat;
    	}
     
    	public void setId_mat(long id_mat) {
    		this.id_mat = id_mat;
    	}
     
    	public long getId_societe() {
    		return id_societe;
    	}
     
    	public void setId_societe(long id_societe) {
    		this.id_societe = id_societe;
    	}
     
    	public long getId_sortie() {
    		return id_sortie;
    	}
     
    	public void setId_sortie(long id_sortie) {
    		this.id_sortie = id_sortie;
    	}
     
    	public Set getIntervention_societes() {
    		return intervention_societes;
    	}
     
    	public void setIntervention_societes(Set intervention_societes) {
    		this.intervention_societes = intervention_societes;
    	}
     
    	public String getN_bon() {
    		return n_bon;
    	}
     
    	public void setN_bon(String n_bon) {
    		this.n_bon = n_bon;
    	}
     
    	public String getObjet() {
    		return objet;
    	}
     
    	public void setObjet(String objet) {
    		this.objet = objet;
    	}
     
    	public String getObs_s() {
    		return obs_s;
    	}
     
    	public void setObs_s(String obs_s) {
    		this.obs_s = obs_s;
    	}
     
     
    }
    et son fichier de mappig et 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
    <?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="data">
    	<class name="BonSortie" table="bon_sortie">
    		<meta attribute="sync-DAO">false</meta>
    		<id name="id_sortie" type="java.lang.Long" column="ID_SORTIE">
    			<generator class="assigned" />
    		</id>
     
    		<property name="n_bon" column="N_BON_S" type="string"
    			not-null="true" length="10" />
    		<property name="date_s" column="DATE_SORTIE" type="string"
    			not-null="true" length="10" />
    		<property name="objet" column="OBJET" type="string"
    			not-null="false" length="30" />
    		<property name="obs_s" column="OBS_SORTIE" type="string"
    			not-null="false" length="50" />
    		<many-to-one name="id_societe" column="ID_SOCIETE"
    			class="Societe" not-null="true">
    		</many-to-one>
    		<many-to-one name="id_interv_soc" column="ID_INTERV_SOC"
    			class="InterventionSociete" not-null="false">
    		</many-to-one>
    		<many-to-one name="id_mat" column="ID_MAT" class="Materiel"
    			not-null="true">
    		</many-to-one>
     
     
    		<set name="intervention_societes" inverse="true">
    			<key column="ID_SORTIE" />
    			<one-to-many class="InterventionSociete" />
    		</set>
     
     
    	</class>
    </hibernate-mapping>
    peut être que c une erreur au niveau des clé étrangères
    votre aide svp.

  2. #2
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Dans ton fichier de mapping tu as déclaré que ton attribut id_societe était de type Societe or dans ton classe il est de type long. C'est ce qui pose problème.
    Tu as le même problème avec plusieurs de tes attributs (id_mat, id_sortie,...).

  3. #3
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 47
    Points : 20
    Points
    20
    Par défaut
    alors vous disez que je dois faire comme ca :
    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
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    package data;
     
    import java.util.HashSet;
    import java.util.Set;
     
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.validator.ValidatorForm;
     
     
    public class BonSortie extends ValidatorForm{
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	private long id_sortie;
     
    	private Materiel id_mat;
     
    	private Societe id_societe;
     
    	private InterventionSociete id_interv_soc;
     
    	private String n_bon;
     
    	private String date_s;
     
    	private String objet;
     
    	private String obs_s;
     
    	private Set intervention_societes = new HashSet();
     
    	public static long getSerialVersionUID() {
    		return serialVersionUID;
    	}
     
    	public String getDate_s() {
    		return date_s;
    	}
     
    	public void setDate_s(String date_s) {
    		this.date_s = date_s;
    	}
     
     
     
    	public InterventionSociete getId_interv_soc() {
    		return id_interv_soc;
    	}
     
    	public void setId_interv_soc(InterventionSociete id_interv_soc) {
    		this.id_interv_soc = id_interv_soc;
    	}
     
    	public Materiel getId_mat() {
    		return id_mat;
    	}
     
    	public void setId_mat(Materiel id_mat) {
    		this.id_mat = id_mat;
    	}
     
    	public Societe getId_societe() {
    		return id_societe;
    	}
     
    	public void setId_societe(Societe id_societe) {
    		this.id_societe = id_societe;
    	}
     
    	public long getId_sortie() {
    		return id_sortie;
    	}
     
    	public void setId_sortie(long id_sortie) {
    		this.id_sortie = id_sortie;
    	}
     
    	public Set getIntervention_societes() {
    		return intervention_societes;
    	}
     
    	public void setIntervention_societes(Set intervention_societes) {
    		this.intervention_societes = intervention_societes;
    	}
     
    	public String getN_bon() {
    		return n_bon;
    	}
     
    	public void setN_bon(String n_bon) {
    		this.n_bon = n_bon;
    	}
     
    	public String getObjet() {
    		return objet;
    	}
     
    	public void setObjet(String objet) {
    		this.objet = objet;
    	}
     
    	public String getObs_s() {
    		return obs_s;
    	}
     
    	public void setObs_s(String obs_s) {
    		this.obs_s = obs_s;
    	}
     
     
    }
    mais lors de l'insertion comment pourrai je affecter des valeurs à ces attributs (id_societe, id_materiel) alors que leurs type sont des classes contenants plusieurs attributs?

  4. #4
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Pour le faire il faudrait que tu fasse quelque chose comme ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    d1.setId_sortie(new InterventionSociete (1234, attribut, attribut,...));
    d1.setId_societe(new Societe (1111, atribut, atrribut,...));
     d1.setN_bon("bonjour");
    d1.setDate_s("1/1/2008");
    si les objets Sociétés et InterventionSociete existe déjà dans ta BD il faut que tu les load puis que tu les passe à ton objet.

  5. #5
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 47
    Points : 20
    Points
    20
    Par défaut
    j'ai besoin de + d'explication ici.
    est ce que ce que vous faites ici c'est d'abord une insertion dans l'autre table qui est relié avec celle dans laquelle je veux inserer?
    merci pour votre patience.

  6. #6
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    85
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 85
    Points : 42
    Points
    42
    Par défaut
    Salut Mustapha (ismouka 3ala esm al7abib ),
    moi aussi je suis débutant en hibernate mais je peux quand même répondre à ta dernière question.
    Il ne s'agit pas d'une insertion dans le code que guigui a ecrit.il n'a fait que préparer l'objet BonSortie à sauvegarder.
    En fait, Mustapha tu ne dois pas réfléchir en SQL . Tu travailles avec hibernate donc il faudra que tu reflechisse en Objet.
    Si tu voudra ajouter un nouveau enregistrement bonSortie il te faudra setter l'objet Societe (et les autres objets) : càd setter aumoins les champs qui ne sont pas nullable.Puis tu fais appel à ta fonction save avec l'objet bonSortie comme paramatere.
    voila.

  7. #7
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 47
    Points : 20
    Points
    20
    Par défaut
    Merci pour votre aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [ZEOSLIB] Problème Insertion dans une table
    Par moscovisci dans le forum Bases de données
    Réponses: 1
    Dernier message: 09/06/2005, 12h05
  2. [interbase6]probleme d'insertion dans une table
    Par macadam314 dans le forum Bases de données
    Réponses: 10
    Dernier message: 22/02/2005, 14h21
  3. [Sybase] Temps d'une insertion dans une table
    Par vsavoir dans le forum Décisions SGBD
    Réponses: 5
    Dernier message: 14/02/2005, 10h04
  4. Extraction d'un .txt et Insertion dans une table
    Par PoPmiSiR dans le forum Access
    Réponses: 8
    Dernier message: 28/10/2004, 19h13
  5. Détection insertion dans une Table
    Par abelman dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 06/07/2004, 14h24

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