@Test
  public void testOverriddenPar() throws Exception {
    File testPackage = buildOverridenPar();
    addPackageToClasspath(testPackage);

    HashMap properties = new HashMap();
    properties.put(AvailableSettings.JTA_DATASOURCE, null);
    Properties p = new Properties();
    p.load(ConfigHelper.getResourceAsStream("/overridenpar.properties"));
    properties.putAll(p);
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("overridenpar", properties);
    EntityManager em = emf.createEntityManager();
    org.hibernate.ejb.test.pack.overridenpar.Bug bug =
        new org.hibernate.ejb.test.pack.overridenpar.Bug();
    bug.setSubject("Allow DS overriding");
    em.getTransaction().begin();
    em.persist(bug);
    em.flush();
    em.remove(bug);
    assertNotNull(bug.getId());
    em.getTransaction().rollback();
    em.close();
    emf.close();
  }
Exemplo n.º 2
0
  public void execute(Target output, Type type) {
    if ((outputFile == null && output == Target.NONE) || type == SchemaExport.Type.NONE) {
      return;
    }
    exceptions.clear();

    LOG.runningHbm2ddlSchemaExport();

    final List<NamedReader> importFileReaders = new ArrayList<NamedReader>();
    for (String currentFile : importFiles.split(",")) {
      try {
        final String resourceName = currentFile.trim();
        InputStream stream = ConfigHelper.getResourceAsStream(resourceName);
        importFileReaders.add(new NamedReader(resourceName, stream));
      } catch (HibernateException e) {
        LOG.debugf("Import file not found: %s", currentFile);
      }
    }

    final List<Exporter> exporters = new ArrayList<Exporter>();
    try {
      // prepare exporters
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      if (output.doScript()) {
        exporters.add(new ScriptExporter());
      }
      if (outputFile != null) {
        exporters.add(new FileExporter(outputFile));
      }
      if (output.doExport()) {
        exporters.add(new DatabaseExporter(connectionHelper, sqlExceptionHelper));
      }

      // perform exporters
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      if (type.doDrop()) {
        perform(dropSQL, exporters);
      }
      if (type.doCreate()) {
        perform(createSQL, exporters);
        if (!importFileReaders.isEmpty()) {
          for (NamedReader namedReader : importFileReaders) {
            importScript(namedReader, exporters);
          }
        }
      }
    } catch (Exception e) {
      exceptions.add(e);
      LOG.schemaExportUnsuccessful(e);
    } finally {
      // release exporters
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      for (Exporter exporter : exporters) {
        try {
          exporter.release();
        } catch (Exception ignore) {
        }
      }

      // release the named readers from import scripts
      for (NamedReader namedReader : importFileReaders) {
        try {
          namedReader.getReader().close();
        } catch (Exception ignore) {
        }
      }
      LOG.schemaExportComplete();
    }
  }