【问题标题】:Insert values not working using Hibernate SessionFactory使用 Hibernate SessionFactory 插入值不起作用
【发布时间】:2015-10-14 19:03:59
【问题描述】:

我在 Spring MVC 中使用 Hibernate 作为 ORM..

在我的模块中,我正在调用一个表单操作来将值存储到表中。但数据并未插入该表中。但是我在日志中没有任何错误。

我尝试使用 sessionFactory 的代码是,

        String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
                + " values (?,?,?)";
        Connection connections = sessionFactory.getCurrentSession().connection();   
        PreparedStatement stmt = connections.prepareStatement(querys);      
        stmt.setString(1, "Auto");
        stmt.setInt(2, 1);
        stmt.setInt(3, 251);
        stmt.executeUpdate();;
        connections.close();

但是以同样的方式,我可以使用 JDBC 驱动程序插入值,如下所示,

        private static final String DB_DRIVER = "org.postgresql.Driver";
        private static final String DB_CONNECTION = "jdbc:postgresql://localhost:5432/xxxxx";
        private static final String DB_USER = "xxxxxx";
        private static final String DB_PASSWORD = "xxxxx";

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;     
        String querys = "INSERT INTO reconcile_process "
                + "(process_type,fk_last_modified_by,fk_bank_stmt_id) VALUES"
                + "(?,?,?)";
        try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(querys);
            preparedStatement.setString(1, "Type");
            preparedStatement.setLong(2, 45);   
            preparedStatement.setLong(3, 251);
        preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }           

        private static Connection getDBConnection() {
            Connection dbConnection = null;
            try {
                Class.forName(DB_DRIVER);
            } catch (ClassNotFoundException e) {
                System.out.println(e.getMessage());
            }
            try {
                dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
                        DB_PASSWORD);
                return dbConnection;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
            return dbConnection;
        }

谁能指出问题出在哪里?

【问题讨论】:

  • 哪里有问题,请显示错误。

标签: java hibernate jdbc


【解决方案1】:

请检查默认包中的hibernate.cfg.xml文件

     <property name="hibernate.connection.autocommit">true</property> 

设置存在。我认为您的操作是正确的,但是由于 Commit 操作您没有得到正确的结果。

或者因为object of domain类你想保存传递给session factory

session.save(obj); 
txn.commit();

【讨论】:

  • 这两种技术都适用于基于注解的休眠。
  • 当您从数据库执行逆向工程时,将生成 hibernate.cfg.xml 填充并将数据库用户名和密码存储在那里只需添加 true property> 行以避免重复 txn.commit()。
  • true 已修复此问题... 设置此属性是否会影响其他事务?因为,它设置为假..
  • 然后在每个事务之后执行提交操作,正如我在答案 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 中解释的那样会话 = sessionFactory.openSession(); txn = session.beginTransaction(); txn.commit();
【解决方案2】:

如果您使用 Hibernate,不妨试试这样的方法,为什么不利用它的优势

    SessionFactory sessionFactory = 
        new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();  
    txn = session.beginTransaction();
    UrTableClass obj = new UrTableClass ();  
    obj.setDescription("Description");
    obj.setName("NAME");
    obj.setAge(28); 
    session.save(obj); 
    txn.commit();

【讨论】:

  • 我在该表中有一个数组类型列。所以我决定使用 Hibernate 进行本机查询。无法在休眠表中使用数组列...
  • 即使这样也很简单,但这一切都取决于你如何设置这些东西以及你现在有多少时间。
  • 我有足够的时间......我很困惑在实体类中获取数组
  • 也没有关于如何使用 Hibernate 将数组插入到 postgres 的明确参考。
【解决方案3】:

为了找到问题的根本原因——我建议以debug 模式启动服务器。然后使用F6逐行调试。这将帮助您跟踪在objects 中传递的值。 如果您一直在使用hibernate,我强烈感觉您遗漏了hibernate.cfg.xml 中需要通过hibernate 保存到数据库中的一些重要配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2018-10-22
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多