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 :

[Postgres] Aucune modification dans la base de données


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Juillet 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 20
    Par défaut [Postgres] Aucune modification dans la base de données
    Bonjour,
    J'aimerais bien insérer de nouveaux éléments dans ma base de donnée Postgres, jai utilisé Hibernate pour le mapping, tout semble être correct, mais je il n'y a aucune modification sur la base de donnée.
    Ci-dessous certains fichiers de mon projet

    hibernate.cfg.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-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/Base1</property>
    		<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    		<property name="hibernate.connection.username">postgres</property>
    		<property name="hibernate.connection.password">postgres</property>
    		<!-- property name="hibernate.connection.pool_size"></property -->
     
    		<!-- dialect for DB2 -->
            <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
     
            <property name="hibernate.show_sql">false</property>
            <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        </session-factory>
    </hibernate-configuration>
    TContact.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
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?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="org.hibernate">
    	<class
    		name="TContact"
    		table="t_contact"
    	>
    		<meta attribute="sync-DAO">false</meta>
     
    		<property
    			name="Id"
    			column="id"
    			type="java.lang.Long"
    			not-null="false"
    			length="10"
    		/>
    		<property
    			name="Nom"
    			column="nom"
    			type="string"
    			not-null="false"
    		/>
    		<property
    			name="Prenom"
    			column="prenom"
    			type="string"
    			not-null="false"
    		/>
    		<property
    			name="Age"
    			column="age"
    			type="string"
    			not-null="false"
    		/>
     
     
    	</class>	
    </hibernate-mapping>
    HibernateUtil.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
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    package org.hibernate;
    import org.apache.catalina.Session;
     
    import org.hibernate.*;
    import org.hibernate.base.*;
    import org.hibernate.cfg.Configuration;
     
    import org.apache.catalina.Session;
     
    import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
     
     
    import java.lang.Object;
     
    import org.hibernate.HibernateException;
     
    import org.hibernate.cfg.Configuration;
     
     
    	public class HibernateUtil {
    	   private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
    	   private static final ThreadLocal<Session> threadlocal=new ThreadLocal<Session>();
    	   private static final Configuration cfg =new Configuration();
    	   private static org.hibernate.SessionFactory sessionFactory;
    	   public static org.hibernate.Session currentSession()throws HibernateException{
    	   Session session=(Session)threadlocal.get();   
    	   if(session==null||!((org.hibernate.Session) session).isOpen()){
    	      if(sessionFactory==null){
    	         try{
    	            cfg.configure(CONFIG_FILE_LOCATION);
    	            sessionFactory=cfg.buildSessionFactory();
     
    	         }
    	         catch(Exception ex){
    	            System.err.println("Erreur");
    	            ex.printStackTrace();
    	         }}
    	         session=(Session) ((sessionFactory!=null)?sessionFactory.openSession():null);
    	         threadlocal.set(session);}
    	         return (org.hibernate.Session) session;
     
    	      }
    	      public static void CloseSession()throws HibernateException{
    	         Session session=(Session)threadlocal.get();
    	         threadlocal.set(null);
    	         if(session!=null){
    	            ((SessionFactory) session).close();
    	         }
    	      }
     
     
     
     
     
    	}
    TContact.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
    package org.hibernate;
     
    import org.hibernate.base.BaseTContact;
     
     
     
    public class TContact extends BaseTContact {
    	private static final long serialVersionUID = 1L;
     
    /*[CONSTRUCTOR MARKER BEGIN]*/
    	public TContact () {
    		super();
    	}
     
    /*[CONSTRUCTOR MARKER END]*/
     
     
    }
    BaseTContact.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
    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
    114
    115
    116
    117
    118
    119
    120
    121
    package org.hibernate.base;
     
    import java.io.Serializable;
     
     
    /**
     * This is an object that contains data related to the t_contact table.
     * Do not modify this class because it will be overwritten if the configuration file
     * related to this class is modified.
     *
     * @hibernate.class
     *  table="t_contact"
     */
     
    public abstract class BaseTContact  implements Serializable {
     
    	public static String REF = "TContact";
    	public static String PROP_NOM = "Nom";
    	public static String PROP_AGE = "Age";
    	public static String PROP_PRENOM = "Prenom";
    	public static String PROP_ID = "Id";
     
     
    	// constructors
    	public BaseTContact () {
    		initialize();
    	}
     
    	protected void initialize () {}
     
     
     
    	// fields
    	private int id;
    	private static java.lang.String nom;
    	private java.lang.String prenom;
    	private Integer age;
     
     
     
     
     
     
    	/**
             * Return the value associated with the column: id
             */
    	public java.lang.Integer getId () {
    		return id;
    	}
     
    	/**
             * Set the value related to the column: id
             * @param i the id value
             */
    	public void setId (int i) {
    		this.id = i;
    	}
     
     
     
    	/**
             * Return the value associated with the column: nom
             */
    	public static java.lang.String getNom () {
    		return nom;
    	}
     
    	/**
             * Set the value related to the column: nom
             * @param nom the nom value
             */
    	public void setNom (java.lang.String nom) {
    		this.nom = nom;
    	}
     
     
     
    	/**
             * Return the value associated with the column: prenom
             */
    	public java.lang.String getPrenom () {
    		return prenom;
    	}
     
    	/**
             * Set the value related to the column: prenom
             * @param prenom the prenom value
             */
    	public void setPrenom (java.lang.String prenom) {
    		this.prenom = prenom;
    	}
     
     
     
    	/**
             * Return the value associated with the column: age
             */
    	public java.lang.Integer getAge () {
    		return age;
    	}
     
    	/**
             * Set the value related to the column: age
             * @param integer the age value
             */
    	public void setAge (Integer integer) {
    		this.age = integer;
    	}
     
     
     
     
     
     
     
    	public String toString () {
    		return super.toString();
    	}
     
     
    }
    Test.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
    import java.util.*;
    import org.hibernate.*;
    //import  org.hibernate.classic.Session;
    import com.tuto.*;
    public class Test {
     
    	public static void main(String[] args){
     
    	Session session = HibernateUtil.currentSession();
    	Transaction tx = session.beginTransaction();
     
    	//TContact contact = new TContact();
    	//contact.setNom("Dupont");
    	//contact.setPrenom("Jean");
    	//contact.setAge(new Integer(44));
    	//session.save(contact);
     
    	TContact contact = new TContact();
    	contact.setNom("sun");
    	contact.setPrenom("yun");
    	contact.setAge(222);
    	session.save(contact);
    	session.flush() ;
    	session.refresh(contact);
    	tx.commit();
     
    	HibernateUtil.CloseSession();
     
     
    	//throws HibernateException {
    	//}
    	}
    }
    Sachant que j'ai exécuter la classe Test comme Java Application et comme JavaBean, je n'ai eu aucun impact dans la base de doonnée.
    Il y a t-il quelqu'un qui peux m'aider?
    Merci beaucoup pour votre éventuelle aide !!

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Bonjour,
    1-
    contact.setAge(222);
    Vu que age est un Integer mais pas un int, tu auras des erreurs sur cette ligne.
    2-
    <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
    Tu utilises postgresql or que ton dialect est pour DB2
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

    3- commit() flush déjà la session si tu n'est pas en "FlushMode.NEVER".
    A+.

  3. #3
    Membre averti
    Inscrit en
    Juillet 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 20
    Par défaut
    Bonjour andry.aime
    En effet, jai pas bien compris le 3ème point
    3- commit() flush déjà la session si tu n'est pas en "FlushMode.NEVER".
    C'est quoi exactement le problème et comment puis-je le corriger?
    Merci,

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Lors de l'appel de la méthode commit(), flush() est appelé, donc il est inutile de l'appeler.
    Soit tu utilises
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    session.save(contact);
    	tx.commit();
    	session.refresh(contact);
    As-tu fait les modifications sur les deux points?
    A+.

  5. #5
    Membre averti
    Inscrit en
    Juillet 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 20
    Par défaut
    Salut,
    Oui j'ai fait les modifications sur les 2 points et puis maintenant j'ai enlevé le flush() mais j'ai toujours le même problème
    J'ai eu les erreurs suivantes
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    	at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:110)
    	at org.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:25)
    	at Test.main(Test.java:9)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	... 3 more

  6. #6
    Membre Expert
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    2 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 938
    Par défaut
    Ton erreur est assez explicite. Il te manque la librairie des logs, le commons-loggins..., tu peux la récuperer normalement dans tous les serveurs d'applis, ou télécharges là sur findJar.com

Discussions similaires

  1. Réponses: 5
    Dernier message: 20/09/2011, 18h55
  2. Réponses: 1
    Dernier message: 29/04/2009, 17h22
  3. Modification dans la base de données
    Par swilhoss dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 26/06/2008, 17h44
  4. Réponses: 1
    Dernier message: 29/04/2008, 10h58
  5. [SQL] aucun ajout dans la base de donnée impossible
    Par gtraxx dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 22/09/2006, 17h49

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