executeUpdate()每24小时执行一次删除查询,以查找过期的项目

时间:2019-02-09 19:52:43

标签: hibernate jax-rs dropwizard

我写了一个Dropwizard java rest应用程序,并且我已经尝试了一个多星期,每24小时执行一次删除查询。我试图用@schedualed和quart来实现它,然后在访问CurrentSession的类时遇到了重大问题。我无法实现hibernateutil类和hibernate.cfg.xml。它没有显示任何明显的错误,只是行不通。 感谢您的帮助。

public class HibernateUtil {

private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        Configuration configuration = new Configuration().configure("/Users/reza/Desktop/awesome-backend-master 2/src/main/resources/hibernate.cfg.xml");
        ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
        registry.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = registry.buildServiceRegistry();

        sessionFactory = configuration.buildSessionFactory();
    }

    return sessionFactory;
}

hibernate.cfg.xml:

   

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.h2.Driver</property>
    <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1</property>
    <property name="connection.username">sa</property><!--./target/example-->
    <property name="connection.password">sa</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>

    <!-- Enable Hibernate's current sessions tracking by thread of execution -->
    <property name="current_session_context_class">managed</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <mapping class="si.klepra.dropwizardbookmarks.core.User"/>
    <mapping class="si.klepra.dropwizardbookmarks.core.Bookmark"/>

</session-factory>

石英调度程序的工作类别:

@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    String queri = "delete from Food where expires < NOW()";

    Query query = HibernateUtil.getSessionFactory().getCurrentSession().createSQLQuery(queri);
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

}

0 个答案:

没有答案