【问题标题】:Records deleted from database after running Java code运行 Java 代码后从数据库中删除的记录
【发布时间】:2018-07-23 19:37:26
【问题描述】:

我已将 MySQL 数据库连接到我的 Java Spring MVC 代码,包括休眠和实体。

我已在 MySQL 中的 Clients 表中添加记录,并成功从 Intelij 获取记录。

但是,运行以下代码后,我的数据库中的所有记录都被删除了。

package com.example.WebAppProcess20.Entities;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.util.ArrayList;

public class dbCheck {
    public static void main (String [] args) throws Exception{
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        ArrayList<ClientsEntity> li = (ArrayList<ClientsEntity>)session.createQuery("from ClientsEntity").list();
        for (ClientsEntity c: li) {
            System.out.println(c.getClientName());
        }
        session.close();
        factory.close();
    }
}

你能解释一下原因吗?

谢谢

【问题讨论】:

    标签: java mysql hibernate


    【解决方案1】:

    您的hibernate.cfg.xml 中的ddl 设置可能设置为在每次运行应用程序时重新创建架构,每次运行时都会擦除所有数据。检查this链接(感谢@notyou)如何修改它以保留以前的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多