使用hibernate创建和删除表

时间:2015-09-29 06:55:03

标签: hibernate postgresql java-ee create-table

我在实现在这些行下定义的方法时遇到了一些问题,无法创建表并删除它。当我调试代码总是转到catch阻止但是创建和删除表的查询在postgreSQL控制台中执行时都能正常工作。知道为什么我有这些错误吗?或者使用Native Query进行这些操作有限制吗?

这里是源代码:

@Transactional(propagation = Propagation.REQUIRED)
      public int createTableTemporaireMesure(DashboardCriteriaBean criteria)
      {
          LOG.debug("début createTableTemporaireMesure");

          int retour = 0;

          // On supprime la table temporaire s'il existe
          dropTableTemporaireMesure();

          String queryString = "CREATE TABLE TMP_MESURE AS (select * from mesures mesure ";
          queryString += " where" + addMesureFilterOptimized(criteria).substring(4) + ")";

          LOG.debug("QueryString : " + queryString);
          Query query = em.createNativeQuery(queryString);

          try {
              query.getResultList();
        } catch (Exception e) {
            LOG.error("Error: " +e.getMessage());
            retour = 1;
        }

          LOG.debug("fin createTableTemporaireMesure");

          return retour;
      }

      @Transactional(propagation = Propagation.REQUIRED)
      public int dropTableTemporaireMesure()
    {
          LOG.debug("début dropTableTemporaireMesure");

          int retour = 0;

        String queryString = "DROP TABLE IF EXISTS TMP_MESURE";
        Query query = em.createNativeQuery(queryString);

        try {
            query.executeUpdate();
        } catch (Exception e) {
            StringBuilder sb = new StringBuilder("Erreur survenue lors de la suppresion de la table : ");
            sb.append("TMP_MESURES");
            sb.append(e.getMessage());
            LOG.error(e);
        }

        LOG.debug("fin dropTableTemporaireMesure");

        return retour;
    }
}

添加:异常详情。

- Drop table
--------------

    javax.persistence.TransactionRequiredException: Executing an update/delete query

    - Create table- Table not dropped cause drop method seems to don't work
    ---------------
    [ WARN] 17:31:59 (JDBCExceptionReporter.java:logExceptions:77)
    SQL Error: 0, SQLState: 42P07

    [ERROR] 17:31:59 (JDBCExceptionReporter.java:logExceptions:78)
    ERROR: relation "tmp_mesure" already exists

    - Create table- Table seems to be created succesfully but SQL Error = 0
    ----------------

    Hibernate: CREATE TABLE TMP_MESURE AS (select * from mesures mesure  where ( mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure in (130,131,132,133,134,135,136,137,138,139,140,150,151,153,154,155,156,157,158,190,230,231,232,233,234,237,269,270,271,275,280,285,299,331,350,351,352,353,354)) or (mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure>=100) and mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure<200)) or (mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure>=200) and mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure<300)) or (mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure>=300) and mesure.id_mesure in (select m2.id_mesure from mesures m2 where m2.code_mesure<400))))
    [ WARN] 17:37:56 (JDBCExceptionReporter.java:logExceptions:77)
    SQL Error: 0, SQLState: 02000

    [ERROR] 17:37:56 (JDBCExceptionReporter.java:logExceptions:78)
    La consulta no retornó ningún resultado. Translation **(Query doesn't returned any result)**

0 个答案:

没有答案