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 :

Hibernate et HSQLDB embarqué


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Par défaut Hibernate et HSQLDB embarqué
    Bonjour,

    J'ai un soucis avec hibernate et la base de données HSQL.
    Quand je me connecte à ma base de donnée (embarquée dans mon application), les fichiers sont bien créés (voir PJ : structureProjet.png).

    Je lance une requète de "create tabl"e puis un "save(Object)" avec hibernate:
    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
     
        HibernateUtil hibernateUtil = new HibernateUtil();
        hibernateUtil.executeSQLCommand("create table survey (id int,name varchar);");
     
        Session session = hibernateUtil.getSession();
     
        Survey survey = new Survey();
        survey.setName("Survey");
        System.out.println(survey.getId());
     
        session.save(survey);
        session.flush();
     
        System.out.println(survey.getId());
        Survey surveyInSession = (Survey) session.get(Survey.class, survey.getId());
        System.out.println(surveyInSession.getName());
     
        session.close();
        hibernateUtil.checkData("select * from survey");
        hibernateUtil.stopServer();
    Ma classe HibernateUtil:
    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
     
        public class HibernateUtil {
    	Server server;
    	Session session;
    	Statement st;
     
    	public HibernateUtil() throws Exception {
    		SessionFactory sessionFactory = new Configuration().configure()
    				.buildSessionFactory();
    		session = sessionFactory.openSession();
     
    		// Load the JDBC driver.
    		server = (Server) Class.forName("org.hsqldb.Server").newInstance();
    		server.setDatabasePath(0, "data/tutorial");
    		server.setDatabaseName(0, "tutorial");
    		server.start();
    		System.out.println("Server Started.");
     
    		// Establish the connection to the database.
    		String url = "jdbc:hsqldb:hsql://localhost/tutorial";
    		Connection conn = DriverManager.getConnection(url, "sa", "");
    		System.out.println("Got Connection.");
    		st = conn.createStatement();
    	}
     
    	public void stopServer() {
    		server.stop();
    	}
     
    	public Session getSession() {
    		return session;
    	}
     
    	public void executeSQLCommand(String sql) throws Exception {
    		st.executeUpdate(sql);
    	}
     
    	public void checkData(String sql) throws Exception {
    		ResultSet rs = st.executeQuery(sql);
    		ResultSetMetaData metadata = rs.getMetaData();
     
    		for (int i = 0; i < metadata.getColumnCount(); i++) {
    			System.out.print("\t" + metadata.getColumnLabel(i + 1));
    		}
    		System.out.println("\n----------------------------------");
     
    		while (rs.next()) {
    			for (int i = 0; i < metadata.getColumnCount(); i++) {
    				Object value = rs.getObject(i + 1);
    				if (value == null) {
    					System.out.print("\t       ");
    				} else {
    					System.out.print("\t" + value.toString().trim());
    				}
    			}
    			System.out.println("");
    		}
    	}
    }
    Ma configuration hibernate:
    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
    <?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>
            <!-- Database connection settings -->
            <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
            <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
            <property name="connection.url">jdbc:hsqldb:hsql://localhost/tutorial</property>
            <property name="connection.username">sa</property>
            <property name="connection.password"></property>
     
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
     
            <!-- Enable Hibernate's current session context -->
            <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
     
            <property name="hibernate.cache.use_second_level_cache">false</property>
            <property name="hibernate.cache.use_query_cache">false</property>
     
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">false</property>
     
            <!-- Mapping files -->
            <mapping resource="Survey.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    Mon fichier de mapping:
    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
    <?xml version="1.0"?>
     
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
     
    <hibernate-mapping>
        <class name="Survey" table="SURVEY">
            <cache usage="read-write"/>
            <id name="id" column="ID" type="java.lang.Long">
                <generator class="increment"/>
            </id>
            <property name="name" column="name" type="java.lang.String"/>
        </class>
    </hibernate-mapping>

    L'exécution ce passe parfaitement bien (même le "select" me retourne mon objet inséré dans la base) mais quand je relance le processus, le "create table" devrait me lancer un exception car la table existe déjà. Au lieu de ça il la recréée.

    je pense que ma première exécution n'a pas écrit dans la base est est resté en mémoire.
    Comment rendre les données persistantes?


    Merci d'avance.

    Spiritkill.
    Images attachées Images attachées  

  2. #2
    Membre éclairé
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Par défaut
    Bonjour,

    J'ai trouvé une solution, il faut accéder à la base avec:
    jdbc:hsqldb:file:data/tutorial

    Puis exécuter la requète "SHUTDOWN" sur la base à la fin de l'application pour que la synchroisation se fasse.


    Merci quand même.

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 30/04/2013, 14h07
  2. [Hibernate + JPA + HSQLdb] Colonne Date
    Par CreatixEA dans le forum Hibernate
    Réponses: 3
    Dernier message: 14/08/2010, 10h49
  3. hibernate et HSQLDB
    Par loicmidy dans le forum Hibernate
    Réponses: 2
    Dernier message: 04/03/2009, 20h35
  4. Hibernate et HSQLDB - pb de performances
    Par tatia34 dans le forum Hibernate
    Réponses: 1
    Dernier message: 05/09/2007, 16h29
  5. Hibernate Eclipse HSQLDB
    Par tlilim dans le forum Hibernate
    Réponses: 8
    Dernier message: 12/08/2007, 15h07

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