hibernate sees no updated data in the table



  • Hello. I'm writing a java program using Hibernate. The code below is to be applied once a few seconds to the table to see if there's any new data on the state column = 0.

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

    Work must be: I put some lines in the table of the frontal state = 0 and the method catches them because it's in the cycle, with a delay of 3 seconds. But the method only covers those lines that have been given the status 0. before programme launch if status is displayed during The program's work, the method's no longer a chain, so he doesn't see that there are new lines with status 0.

    I tried to use StatelessSession instead of Session, and I tried after HQL request to sign the session.setCacheMode(CacheMode.IGNORE), but it didn't work.

    Here's the 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;
    

    }

    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>

    There's nothing else in the cfg.

    Help me figure out why this is happening.



  • I've dealt with my problem.
    Shortly, the replacement of openSession() on getCurrentSession() and commit at the end of the request.

    So if it's all about:

    In hibernate.cfg.xml, add this line.

    <property name="hibernate.current_session_context_class">thread</property>
    

    Dao looks like this.

         class Dao {
            public List<MyTable> getNewRows() throws Exception {                      
               Session session = HibernateUtil.getSessionFactory().getCurrentSession();
               session.getTransaction().begin();
               List<MyTable> result = session.createQuery("from MyTable where state = 0 ").list();
               session.getTransaction().commit();       
               return result;
            }
        }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2