2013-07-04 24 views
0

我使用Hibernate框架編碼算法來瀏覽表並返回多行。算法刪除表中的數據而不是提取

但是,該算法會刪除表中的內容,則此查詢的返回值爲null

ArrayList<Compte> list = null; 
    try { 
    Session session = HibernateUtils.getSession(); 
    Transaction tx = session.beginTransaction(); 
    Query q = s.createQuery("from Compte where compte_utilisateuridentifiant = :y"); 
    q.setString("y", identifiant); 
    list = (ArrayList<Compte>) q.list(); 
    tx.commit(); 
    } catch (HibernateException e) { 

    } finally { 

    } 

    for (Compte c: list) 
     System.out.println("[rib] = " + c.getRib() + "\t" + 
       "[title] = " + c.getLibelle() + "\t" + 
       "[dateC] = " + c.getDateCréation() + "\t"); 
    return list; 

這裏是CFG文件:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
    <!-- Paramètres de connexion à la base de données --> 
    <property name="connection.driver_class">org.postgresql.Driver</property> 
    <property name="connection.url">jdbc:postgresql://localhost:5432/bh</property> 
    <property name="connection.username">postgres</property> 
    <property name="connection.password">esct</property> 
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

    <!-- Comportement pour la conservation des tables --> 
    <property name="hbm2ddl.auto">create</property> 

    <!-- Activation : affichage en console, commentées et formatées --> 
    <property name="show_sql">true</property> 
    <property name="hibernate.format_sql">true</property> 
    <property name="use_sql_comments">true</property> 

    <!-- Fichiers à mapper --> 
    <mapping class="tn.bh.jpa.Compte" /> 
    <mapping class="tn.bh.jpa.Mouvement_Compte" /> 
    <mapping class="tn.bh.jpa.Solde" /> 
    <mapping class="tn.bh.jpa.User" /> 
    <mapping class="tn.bh.jpa.Virement" /> 

有人能幫助我嗎? 謝謝:)

+0

你可以粘貼cfg文件嗎?在提交事務後關閉會話。 – zerocool

+0

夥計們,請幫助我 – Gentuzos

回答

0

更改以下屬性可能會有幫助:

<property name="hbm2ddl.auto">update</property> 

原因:我認爲你是通過重新啓動休眠運行此查詢,因爲屬性設置爲創建,一旦冬眠重新啓動,它創建再次完成模式,並執行您的查詢。由於沒有數據(因爲創建了新的模式,並且在查詢執行前沒有插入/保存到表),所以最終不會得到任何結果。

請注意,我在這裏假設的東西。

相關問題