-1

I am using Hibernate as an ORM for a server daemon.

This code returns no items if the table associated is empty at run time but has an item added outside of 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;
}

Here is the hibernate config

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

If I create an object and persist it from within the application it works fine, If there is already data in the table and I insert the data it finds the other objects, the issue arises as soon as the table is empty during run time.

This is a big issue because another application handles the inserting of data for these objects.

I am using hibernate 4.2.7.Final with Java 1.7(jdk1.7.0_45) in eclipse.

Largo Usagi
  • 1
  • 1
  • 2
  • 4
    Do you have a second-level cache? Are you sure the other application has committed its changes? – JB Nizet Nov 20 '13 at 22:06
  • Well the other application is currently MySQL Workbench, so yes, if I restart the daemon and it checks the table it sees the new record. I will edit in my hibernate config – Largo Usagi Nov 20 '13 at 23:25
  • This might help you http://stackoverflow.com/questions/3827704/how-to-disable-hibernate-caching – Ean V Nov 21 '13 at 00:42
  • Tried that, still didn't work. If I manually insert a new record while there is still data in the queue it will find the next one though. Really frustrating. – Largo Usagi Nov 21 '13 at 01:19

1 Answers1

-2

You can resolve this problem since hibernate.cfg.xml

adding this tag:

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343