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 :

Aucune sauvegarde en db


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité
    Invité(e)
    Par défaut Aucune sauvegarde en db
    Bonjour,

    Je suis face à un problème assez bloquant; aucunes de mes modification n'est répercutée dans ma base de données.
    Par contre le temps de l'exécution de l'application tout semble fonctionner correctement, comme si hibernate travaillait sur un db en mémoire et non pas sur ma db physique.

    Pour configurer hibernate, j'utilise un fichier xml et un fichier de properties personnel. J'ai une petite partie de code chargée de "fusionner" mon fivhier de properties dans un objet Properties utilisable par Hibernate.

    hibernate.cfg.xml :
    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.pool_size">2</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    </session-factory>
    </hibernate-configuration>
    Le code de "fusion" :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Properties hibernateProperties = new Properties();
    		String url = new StringBuilder("jdbc:postgresql://")
    			.append(properties.getProperty("jdbc.host.url"))
    			.append(":").append(properties.getProperty("jdbc.host.port","5432"))
    			.append("/").append(properties.getProperty("jdbc.db.name","MYDB"))
    			.toString();
    		hibernateProperties.put("hibernate.connection.url", url);
    		hibernateProperties.put("hibernate.connection.username", properties.get("jdbc.user.name"));
    		hibernateProperties.put("hibernate.connection.password", properties.get("jdbc.user.password"));
     
    		hibUtils = new HibernateUtil(hibernateProperties, new File("./conf/hibernate.cfg.xml"));
    Ma classe Hibernate Util :
    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
    public HibernateUtil(Properties properties, File hibernateCfg) {			
    		Configuration cfg = new Configuration()
    			// Mappings		
    			.addResource("...")
     
    			// Dynamic properties (user editables : url, username, password)
    			.setProperties(properties)
     
    			// Core properties (from hibernate.cfg.xml)
    			.configure(hibernateCfg);
    		try {
    			sessionFactory = cfg.buildSessionFactory();
    		} catch (Throwable e) {
    			throw new ExceptionInInitializerError(e);
    		}
    	}
    La configuration fonctionne (Hibernate se connecte et récupère le contenu de ma db) mais, comme dis au début, aucune modification n'est écrite dans la db.

    Voici un exemple de logs crées par Hibernate lors de la mise à jour d'un objet :
    UPDATING resource
    2008-09-09 14:17:15,359 DEBUG [xx.yy.zz.dao.impl.spring.hibernate.GenericHibernateDao] - Updating Resource 'Resource /testresource/* with roles : [xx.yy.commons.beans.Role@719f1f, xx.yy.commons.beans.Role@1474e45, xx.yy.commons.beans.Role@63a721]'.
    2008-09-09 14:17:15,359 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
    2008-09-09 14:17:15,359 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 12209626353
    2008-09-09 14:17:15,375 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - updating detached instance
    2008-09-09 14:17:15,375 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - updating [xx.yy.zz.security.Resource#/testresource/*]
    2008-09-09 14:17:15,375 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - updating [xx.yy.zz.security.Resource#/testresource/*]
    2008-09-09 14:17:15,390 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
    2008-09-09 14:17:15,390 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - flushing session
    2008-09-09 14:17:15,390 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - processing flush-time cascades
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - dirty checking collections
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushing entities and processing referenced collections
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.DefaultFlushEntityEventListener] - Updating entity: [xx.yy.zz.security.Resource#/testresource/*]
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.engine.Collections] - Collection found: [xx.yy.zz.security.Resource.roles#/testresource/*], was: [xx.yy.zz.security.Resource.roles#/testresource/*] (initialized)
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Processing unreferenced collections
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Scheduling collection removes/(re)creates/updates
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.pretty.Printer] - listing entities:
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.pretty.Printer] - xx.yy.zz.security.Resource{description=All resources relative to the student folder, roles=[xx.yy.commons.beans.Role#Utilisateurs, xx.yy.commons.beans.Role#Administrateurs, xx.yy.commons.beans.Role#Demonstration], url=/testresource/*}
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - executing flush
    2008-09-09 14:17:15,406 DEBUG [org.hibernate.jdbc.ConnectionManager] - registering flush begin
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Updating entity: [xx.yy.zz.security.Resource#/testresource/*]
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] - total checked-out connections: 0
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] - using pooled JDBC connection, pool size: 0
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.SQL] - update resources set description=? where url=?
    Hibernate:
    update resources
    set description=?
    where url=?
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Dehydrating entity: [xx.yy.zz.security.Resource#/testresource/*]
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.type.StringType] - binding 'All resources relative to the student folder' to parameter: 1
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.type.StringType] - binding '/testresource/*' to parameter: 2
    2008-09-09 14:17:15,421 DEBUG [org.hibernate.jdbc.AbstractBatcher] - Executing batch size: 1
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.ConnectionManager] - registering flush end
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - post flush
    2008-09-09 14:17:15,437 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.impl.SessionImpl] - closing session
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.ConnectionManager] - performing cleanup
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] - returning connection to pool, pool size: 1
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction completion
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.jdbc.ConnectionManager] - aggressively releasing JDBC connection
    2008-09-09 14:17:15,437 DEBUG [org.hibernate.impl.SessionImpl] - after transaction completion
    L'un d'entre vous à t'il une idée ?

    Merci

  2. #2
    Membre expérimenté Avatar de bidi
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    262
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2004
    Messages : 262
    Par défaut
    Hello,

    A mon avis tu n'as pas ouvert de transaction autour de ton save :-)

  3. #3
    Invité
    Invité(e)
    Par défaut
    Non, en effet mais je n'ai pas besoin de faire celà dans une transaction..

    Mes DAOS sont récupérés d'un projet existant qui utilise Spring et les transactions par AOP. Dans mon cas je n'ai pas besoin de mettre en places de transactions..

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    si tu active l'autocommit sur ton connecteur sql (je sais, c'est crade mais bon), est-ce que çà passe?

  5. #5
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par tchize_ Voir le message
    si tu active l'autocommit sur ton connecteur sql (je sais, c'est crade mais bon), est-ce que çà passe?
    Non...

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    383
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 383
    Par défaut
    Tu veux dire que les update SQL ne sont jamais effectués ou qu'ils ne sont pas exécutés tout de suite ?

    Avec Hibernate, pas besoin de faire un save() pour modifier un objer, il suffit de modifier l'Entité en mémoire, et Hibernate répercute la modification en base automatiquement.
    Par contre, Hibernate choisit le meilleur moment pour le faire (par exemple juste avant une requête qui remonterait cette entité).

    Pour forcer l'exécution immédiate des update, il faut faire un flush().

Discussions similaires

  1. Réponses: 7
    Dernier message: 22/06/2010, 15h15
  2. [ADO] Sauvegarde / lecture de recordset
    Par SpaceFrog dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 20/09/2002, 16h54
  3. Sauvegarder une surface dans un fichier
    Par Freakazoid dans le forum DirectX
    Réponses: 6
    Dernier message: 18/08/2002, 15h23
  4. [Kylix] Sauvegarde de donnée utilisateur....
    Par Eclypse dans le forum EDI
    Réponses: 1
    Dernier message: 11/05/2002, 17h21

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