【问题标题】:Hibernate does not see data inserted from external applicationHibernate 看不到从外部应用程序插入的数据
【发布时间】:2016-11-07 15:43:56
【问题描述】:

我将 Hibernate 用作服务器守护程序的 ORM。

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

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

这里是休眠配置

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

【问题讨论】:

  • 你有二级缓存吗?您确定其他应用程序已提交其更改吗?
  • 好吧,另一个应用程序当前是 MySQL Workbench,所以是的,如果我重新启动守护程序并检查它会看到新记录的表。我将在我的休眠配置中进行编辑
  • 试过了,还是不行。如果我在队列中仍有数据时手动插入一条新记录,它会找到下一条记录。真令人沮丧。

标签: java hibernate


【解决方案1】:

您可以解决此问题,因为 hibernate.cfg.xml

添加此标签:

&lt;property name="hibernate.connection.autocommit"&gt;true&lt;/property&gt;

【讨论】:

  • Fwiw,这可能是正确的答案...如果其他应用程序也在使用 Hibernate。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多