Hello. I am writing a java program using Hibernate. The code below, should refer once every few seconds to a table to check if there is new data with the state = 0 column.
class Dao { public List<MyTable> getNewRows() throws Exception { Session session = null; List<MyTable> result; try { session = HibernateUtil.getSessionFactory().openSession(); result = session.createQuery("from MyTable where state = 0 ").list(); } catch (Exception e) { throw e; } finally { session.close(); } return result; } }
It should work like this: I expose some rows in the table with a third-party program state = 0 and the method should pick them up, because it is called in a cycle, with a delay of 3 seconds. But the method picks up only those lines that were set to status 0 before starting the program, but if you set up statuses while the program is running, then the method no longer “clings”, that is, it does not see that new lines have appeared with status 0.
I tried to use StatelessSession instead of Session, and after the HQL request I tried to add session.setCacheMode (CacheMode.IGNORE), but it did not help.
here is util
public class HibernateUtil { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { // loads configuration and mappings Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // builds a session factory from the service registry sessionFactory = configuration.buildSessionFactory(serviceRegistry); } return sessionFactory; }
}
here is cfg
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property> <property name="hibernate.connection.username">login</property> <property name="hibernate.connection.password">pass</property>
In addition to these lines in cfg There is nothing else.
Help me figure out why this is happening.