2012-03-17 8 views
1

私はJavaで4層のWebアプリケーションを構築しようとしています。 ORMとMySQLデータベース用のHibernate Frameworkを使用しています。 テーブル「プロジェクト」は、テーブル「アイデア」と一対一の関係にあります。私は何をしようとしている、次の関数を使用してのアイデアのリスト(またはセット)を取得することです:最初はMySQL/Hibernateの一貫性のないデータ構造

public static List getProjectIdeas(int projectId) 
    { 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     session.beginTransaction(); 
     Query q = session.createQuery("from Idea where id_project=" + projectId); 
     List<Idea> ideaList = (List<Idea>) q.list(); 
     System.out.println(ideaList.size()); 
     return ideaList; 
    } 

を、それが正常に動作し、私は、データベースからすべてのアイデアを得ます。しかし、プロジェクトに新しいアイデアを追加してから、再び関数を呼び出すと、プロジェクトに関連するアイデアの完全なリストは必ずしも得られません。 q.list()から受け取るリストのサイズは、挿入前のデータベースに格納されているアイデアの数と実際のアイデアの数によって異なります。 これは私がデータベースにアイデアを挿入するために使用する機能です。

public static void writeObject(Object object) 
    { 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     Transaction tx = session.beginTransaction(); 
     session.saveOrUpdate(object); 
     tx.commit(); 
    } 

これが私の最初の休止状態のプロジェクトですので、設定ファイルに何か問題がある場合、私は知らない。

<?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> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">***/ideator</property> 
    <property name="hibernate.connection.username">***</property> 
    <property name="hibernate.connection.password">***</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property> 
    <mapping resource="mapping/Category.hbm.xml"/> 
    <mapping resource="mapping/Rating.hbm.xml"/> 
    <mapping resource="mapping/Idea.hbm.xml"/> 
    <mapping resource="mapping/Participation.hbm.xml"/> 
    <mapping resource="mapping/Project.hbm.xml"/> 
    <mapping resource="mapping/Criteria.hbm.xml"/> 
    <mapping resource="mapping/User.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

ご協力いただきありがとうございます。

答えて

0

[OK]をCurrentSession方法に洗い流し、私はバグを修正することができました。どうやら、getCurrentSessionを使うのは良い考えではありませんでした。セッションを自分で管理してください。

Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction tx = null; 
     Idea idea = null; 

     try { 
      tx = session.beginTransaction(); 
      idea = (Idea) session.get(Idea.class, id); 
      tx.commit(); 
     } catch (Exception e) { 
      if(tx != null) { 
       tx.rollback(); 
      } 
     } finally { 
      session.flush(); 
     } 

     return idea; 

ケースが閉じました。

0

ちょうどあなたが知っている、休止状態はクールです。一度それを働かせれば、休止状態で本当に素敵で簡単です。しかし、スタートアップの問題があります。あなたは正しい軌道に乗っています。上のコードサンプルは本当に素晴らしいです。それは良い質問です。ようこそstackoverflowへ。

これは私がやることであり、私のために働くものです。

コンストラクタ

Configuration configuration = new Configuration().configure() ;      
sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder() .buildServiceRegistry()); 

データベース

public void flush(Object dataStore) throws DidNotSaveRequestSomeRandomError { 
    Transaction txD; 
    Session session; 
    session = currentSession(); 
    txD = session.beginTransaction(); 
    session.saveOrUpdate(dataStore); 
    try { 
     txD.commit(); 
    } catch (ConstraintViolationException e) { 
     log.error("Unable to store data from "+dataStore.getClass().toString()) ; 
     throw new DidNotSaveRequestSomeRandomError(dataStore); 
    } catch (TransactionException e) { 
     log.debug("txD state isActive" + txD.isActive() + " txD is participating" + txD.isParticipating()); 
     log.debug(e); 
    } finally { 
     txD = null; 
     session.close(); 
    } 
}      

public final ThreadLocal session = new ThreadLocal(); 
public Session currentSession() { 
    Session s = (Session) session.get(); 
    // Open a new Session, if this thread has none yet 
    if (s == null || !s.isOpen()) { 
     s = sessionFactory.openSession(); 
     // Store it in the ThreadLocal variable 
     session.set(s); 
    } 
    return s; 
} 
+0

あなたのアンサーに感謝しますが、あなたのcurrentSessionの代わりにHibernateUtil.getSessionFactory()。getCurrentSession()を使用するため、これらの関数をどこで実装するのかは静かではありません。私はすべてフラッシュを使用しない、私は休止状態が自動的にそれを行うと思います。 – Phil

+0

私はHibernate Utilを使用しません。そして、フラッシュは私自身の実装です。 Hibernateはフラッシュを持たず、コミットしています。 – Siddharth

関連する問題