我想在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>
在此先感谢您的帮助。
感谢您的anwser,但我并不清楚在哪里实现这些功能,因为不是我的currentSession,而是使用HibernateUtil.getSessionFactory()。getCurrentSession()。我根本不使用flush,我认为hibernate会自动执行。 – Phil 2012-03-18 15:43:54
我不使用Hibernate Util。冲洗是我自己的实现。 Hibernate没有刷新,它有一个提交。 – Siddharth 2012-03-19 14:47:35