Hibernate 删除所有行并从 1 开始索引。

huangapple go评论71阅读模式
英文:

Hibernate delete all rows and start indexing from 1

问题

我想每晚更新我的数据库以获取新值。简单地说,通过删除所有行并存储新数据来'重新开始'。

到目前为止,我使用以下两个查询来实现:

public void deleteProduct() {
    Transaction transaction = null;
    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        transaction = session.beginTransaction();
        session.createQuery("DELETE FROM Product").executeUpdate();
    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
        e.printStackTrace();
    }
}

以及

public void saveProduct(Product student) {
    Transaction transaction = null;
    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        transaction = session.beginTransaction();
        // 保存学生对象
        session.save(student);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
        e.printStackTrace();
    }
}

对于每个元素。

我所遇到的问题是,在删除和插入新值后,记录的索引不是从零开始,而是从删除之前的最后一行的编号开始。

英文:

I want to update my database every night for new values. Simply speaking 'start over' by deleting all rows and store new data.

So far I'm doing it using these two queries:

public void deleteProduct() {
        Transaction transaction = null;
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            transaction = session.beginTransaction();
            session.createQuery("DELETE FROM Product").executeUpdate();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

And

public void saveProduct(Product student) {
        Transaction transaction = null;
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            transaction = session.beginTransaction();
            // save the student object
            session.save(student);
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

For each element.

The problem I'm dealing with is that after deletion and insertion of new values.
The indexes of my records are started not from zero but from the number of the last row before deletion.

答案1

得分: 1

你可以删除这些表,并使用同样简单的SQL语句重新构建它们。或者,你可以忽略你的键值,只要它是唯一的并且能够正常工作。可以将自己的列作为计数器,并让键值作为主键。这里有几种替代方案。但是如果你真的希望从1开始重新计数,我会重新构建表格。

英文:

You could delete the tables, and rebuild them with an equally simple sql statement. Or, you could ignore your key value, so long as it's unique and doing it's job. Make your own column as a counter and let the key be the key. That's several alternatives right there. But I would rebuild the table if you really are liking that key to start over at 1 again.

huangapple
  • 本文由 发表于 2020年5月3日 23:31:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定