Hibernate看不到从外部应用程序插入的数据

时间:2013-11-20 22:00:33

标签: java hibernate

我正在使用Hibernate作为服务器守护程序的ORM。

如果关联的表在运行时为空但在hibernate之外添加了一个项,则此代码不返回任何项。

public static List<QueueItem> queue()
{
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();


    session.flush();
    session.clear();

    @SuppressWarnings("unchecked")
    List<QueueItem> topList = session.createQuery("FROM QueueItem i ORDER BY i.id asc").setMaxResults(3).list();

    session.close();
    return topList;
}

这是hibernate配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/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">jdbc:mysql://10.10.1.4/lanjukebox_test</property>
   <property name="hibernate.connection.username">ussqldev</property>
   <property name="hibernate.connection.password">ussqldev</property>

   <property name="hibernate.jdbc.batch_size">25</property>

   <property name="connection.pool_size">1</property>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="current_session_context_class">thread</property>


   <property name="hibernate.cache.use_second_level_cache">false</property>

   <property name="show_sql">false</property>

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

   <!-- List of XML mapping files -->
   <mapping resource="usagisoft/lan/jukebox/domain/entities/Song.hbm.xml"/>
   <mapping resource="usagisoft/lan/jukebox/domain/entities/Id3.hbm.xml"/>
   <mapping resource="usagisoft/lan/jukebox/domain/entities/QueueItem.hbm.xml"/>
   <mapping resource="usagisoft/lan/jukebox/domain/entities/Vote.hbm.xml"/>
   <mapping resource="usagisoft/lan/jukebox/domain/entities/User.hbm.xml"/>

</session-factory>
</hibernate-configuration>

如果我创建一个对象并在应用程序中持久化它可以正常工作,如果表中已有数据并且我插入数据它会找到其他对象,那么一旦表在运行期间为空就会出现问题时间。

这是一个很大的问题,因为另一个应用程序会处理这些对象的数据插入。

我在eclipse中使用hibernate 4.2.7.Final和Java 1.7(jdk1.7.0_45)。

1 个答案:

答案 0 :(得分:-2)

您可以解决此问题 的hibernate.cfg.xml

添加此标记:

  

<property name="hibernate.connection.autocommit">true</property>