【问题标题】:how to write insertinto command in hibernate criteria如何在休眠条件中编写 insertinto 命令
【发布时间】:2011-06-05 06:42:06
【问题描述】:

我想在 Hibernate Criteria 中编写下面的 InsertInto 查询。 有什么建议么 .. 感谢帮助

        sql = "insert into selectedresumes  values('" + companyId + "','"
        + resumeId + "','" + resumeStatusId + "','" + jobId + "')";

【问题讨论】:

    标签: hibernate criteria


    【解决方案1】:

    很遗憾,你做不到。

    根据 Hibernate 文档

    http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#batch-direct

    只有 INSERT INTO ... SELECT ... 支持表格;不是 INSERT INTO ... VALUES ...形式。

    所以你只需要创建 Object 并使用 Hibernate 保存它,它应该看起来像这样

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    Resume selectedresumes  = new Resume();
    //set all resume values
    session.save(selectedresumes);
    tx.commit();
    session.close();
    

    【讨论】:

    • 感谢您的帮助,我正在做同样的事情,但在这一行 tx.commit();它抛出异常 org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update 不知道是什么原因。谢谢
    【解决方案2】:

    您应该将查询文件映射到 pojo 类而不是 SQL 或 MySQL 数据库表。

    像下面一样,Employee pojo 对象有两个字段 empNo,empName 要插入,如下所示。 查询query = session.createQuery("插入Employee(empNo, empName)");

    int 结果 = query.executeUpdate();

    参考这个例子

    http://howtodoinjava.com/hibernate/hibernate-insert-query-tutorial/

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2021-06-14
      • 2012-08-05
      • 2020-04-13
      • 1970-01-01
      • 2012-05-06
      • 2014-06-23
      • 1970-01-01
      相关资源
      最近更新 更多