2012-03-17 46 views
1

我想在Java中构建一个4层Web应用程序。我为ORM和MySQL数据库使用Hibernate Framework。 表“项目”与表“想法”具有一对多关系。我想要做的,是让使用下面的函数思想的列表(或一组):MySQL /休眠不一致的数据结构

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(); 
    } 

这是我的第一个Hibernate项目,所以我不知道是否有什么不对的配置文件:

<?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

好吧,我是能够解决的bug。显然,使用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

就这么你知道,hibernate很酷。一旦你得到它的工作,事情真的很好,容易与休眠。但有些问题正在启动。所以你在正确的轨道上。上面的代码示例非常好。这是一个很好的问题。欢迎来到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(); 
    } 
}      

CurrentSession方法

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

感谢您的anwser,但我并不清楚在哪里实现这些功能,因为不是我的currentSession,而是使用HibernateUtil.getSessionFactory()。getCurrentSession()。我根本不使用flush,我认为hibernate会自动执行。 – Phil 2012-03-18 15:43:54

+0

我不使用Hibernate Util。冲洗是我自己的实现。 Hibernate没有刷新,它有一个提交。 – Siddharth 2012-03-19 14:47:35