@Test public void testConnection() throws IOException, SQLException { final Properties settings = createDefaultSettings(); final EntityImporter entityImporter = new EntityImporter(settings); final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test", settings); try { final EntityManager em = emf.createEntityManager(); try { try (Connection connection = em.unwrap(SessionImpl.class).getJdbcConnectionAccess() .obtainConnection()) { entityImporter.importData(connection); connection.commit(); } final List<TestEntity> entities = em.createQuery("SELECT e FROM TestEntity e", TestEntity.class) .getResultList(); final int expectedEntities = 9 + 3 + 1 + 1 + 2; assertThat(entities).hasSize(expectedEntities); } finally { em.close(); } } finally { emf.close(); } }
public void importData() throws IOException { String statementsWriter = getSettings().getProperty(STATEMENTS_WRITER_KEY, FileStatementsWriter.class.getSimpleName()); if (statementsWriter.indexOf('.') < 0) { statementsWriter = "org.fastnate.generator.statements." + statementsWriter; } try { log.info("Using {} as statements writer", statementsWriter); final Class<StatementsWriter> statementsWriterClass = (Class<StatementsWriter>) Class .forName(statementsWriter); final StatementsWriter writer = statementsWriterClass.getConstructor(GeneratorContext.class) .newInstance(this.context); try (EntitySqlGenerator generator = new EntitySqlGenerator(this.context, writer)) { if (writer instanceof ConnectedStatementsWriter) { importData(generator, ((ConnectedStatementsWriter) writer).getConnection()); } else { importData(generator); } } } catch (final SQLException e) { throw new IOException(e); } catch (final ClassNotFoundException e) { throw new IllegalArgumentException("Unknown statements writer: " + statementsWriter, e); } catch (final NoSuchMethodException e) { throw new IllegalArgumentException( "Statements writer needs a (GeneratorContext) constructor: " + statementsWriter, e); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | SecurityException e) { throw new IllegalArgumentException("Could not build statements writer: " + statementsWriter, e); } }
EntityImporter { public void importData() throws IOException { String statementsWriter = getSettings().getProperty(STATEMENTS_WRITER_KEY, FileStatementsWriter.class.getSimpleName()); if (statementsWriter.indexOf('.') < 0) { statementsWriter = "org.fastnate.generator.statements." + statementsWriter; } try { log.info("Using {} as statements writer", statementsWriter); final Class<StatementsWriter> statementsWriterClass = (Class<StatementsWriter>) Class .forName(statementsWriter); final StatementsWriter writer = statementsWriterClass.getConstructor(GeneratorContext.class) .newInstance(this.context); try (EntitySqlGenerator generator = new EntitySqlGenerator(this.context, writer)) { if (writer instanceof ConnectedStatementsWriter) { importData(generator, ((ConnectedStatementsWriter) writer).getConnection()); } else { importData(generator); } } } catch (final SQLException e) { throw new IOException(e); } catch (final ClassNotFoundException e) { throw new IllegalArgumentException("Unknown statements writer: " + statementsWriter, e); } catch (final NoSuchMethodException e) { throw new IllegalArgumentException( "Statements writer needs a (GeneratorContext) constructor: " + statementsWriter, e); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | SecurityException e) { throw new IllegalArgumentException("Could not build statements writer: " + statementsWriter, e); } } }
EntityImporter { public void importData() throws IOException { String statementsWriter = getSettings().getProperty(STATEMENTS_WRITER_KEY, FileStatementsWriter.class.getSimpleName()); if (statementsWriter.indexOf('.') < 0) { statementsWriter = "org.fastnate.generator.statements." + statementsWriter; } try { log.info("Using {} as statements writer", statementsWriter); final Class<StatementsWriter> statementsWriterClass = (Class<StatementsWriter>) Class .forName(statementsWriter); final StatementsWriter writer = statementsWriterClass.getConstructor(GeneratorContext.class) .newInstance(this.context); try (EntitySqlGenerator generator = new EntitySqlGenerator(this.context, writer)) { if (writer instanceof ConnectedStatementsWriter) { importData(generator, ((ConnectedStatementsWriter) writer).getConnection()); } else { importData(generator); } } } catch (final SQLException e) { throw new IOException(e); } catch (final ClassNotFoundException e) { throw new IllegalArgumentException("Unknown statements writer: " + statementsWriter, e); } catch (final NoSuchMethodException e) { throw new IllegalArgumentException( "Statements writer needs a (GeneratorContext) constructor: " + statementsWriter, e); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | SecurityException e) { throw new IllegalArgumentException("Could not build statements writer: " + statementsWriter, e); } } EntityImporter();  EntityImporter(final GeneratorContext context);  EntityImporter(final Properties settings); }
EntityImporter { public void importData() throws IOException { String statementsWriter = getSettings().getProperty(STATEMENTS_WRITER_KEY, FileStatementsWriter.class.getSimpleName()); if (statementsWriter.indexOf('.') < 0) { statementsWriter = "org.fastnate.generator.statements." + statementsWriter; } try { log.info("Using {} as statements writer", statementsWriter); final Class<StatementsWriter> statementsWriterClass = (Class<StatementsWriter>) Class .forName(statementsWriter); final StatementsWriter writer = statementsWriterClass.getConstructor(GeneratorContext.class) .newInstance(this.context); try (EntitySqlGenerator generator = new EntitySqlGenerator(this.context, writer)) { if (writer instanceof ConnectedStatementsWriter) { importData(generator, ((ConnectedStatementsWriter) writer).getConnection()); } else { importData(generator); } } } catch (final SQLException e) { throw new IOException(e); } catch (final ClassNotFoundException e) { throw new IllegalArgumentException("Unknown statements writer: " + statementsWriter, e); } catch (final NoSuchMethodException e) { throw new IllegalArgumentException( "Statements writer needs a (GeneratorContext) constructor: " + statementsWriter, e); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | SecurityException e) { throw new IllegalArgumentException("Could not build statements writer: " + statementsWriter, e); } } EntityImporter();  EntityImporter(final GeneratorContext context);  EntityImporter(final Properties settings); static void main(final String[] args); void addDataProvider(final DataProvider provider); void addDataProvider(final DataProvider provider, final int maximumOrderOfDepenendencies); P findDataProvider(final Class<P> providerClass); Properties getSettings(); void importData(); void importData(final Connection connection); void importData(final EntitySqlGenerator generator); void importData(final File targetFile); void importData(final Writer writer); }
EntityImporter { public void importData() throws IOException { String statementsWriter = getSettings().getProperty(STATEMENTS_WRITER_KEY, FileStatementsWriter.class.getSimpleName()); if (statementsWriter.indexOf('.') < 0) { statementsWriter = "org.fastnate.generator.statements." + statementsWriter; } try { log.info("Using {} as statements writer", statementsWriter); final Class<StatementsWriter> statementsWriterClass = (Class<StatementsWriter>) Class .forName(statementsWriter); final StatementsWriter writer = statementsWriterClass.getConstructor(GeneratorContext.class) .newInstance(this.context); try (EntitySqlGenerator generator = new EntitySqlGenerator(this.context, writer)) { if (writer instanceof ConnectedStatementsWriter) { importData(generator, ((ConnectedStatementsWriter) writer).getConnection()); } else { importData(generator); } } } catch (final SQLException e) { throw new IOException(e); } catch (final ClassNotFoundException e) { throw new IllegalArgumentException("Unknown statements writer: " + statementsWriter, e); } catch (final NoSuchMethodException e) { throw new IllegalArgumentException( "Statements writer needs a (GeneratorContext) constructor: " + statementsWriter, e); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | SecurityException e) { throw new IllegalArgumentException("Could not build statements writer: " + statementsWriter, e); } } EntityImporter();  EntityImporter(final GeneratorContext context);  EntityImporter(final Properties settings); static void main(final String[] args); void addDataProvider(final DataProvider provider); void addDataProvider(final DataProvider provider, final int maximumOrderOfDepenendencies); P findDataProvider(final Class<P> providerClass); Properties getSettings(); void importData(); void importData(final Connection connection); void importData(final EntitySqlGenerator generator); void importData(final File targetFile); void importData(final Writer writer); static final String GENERATION_ABORTED_MESSAGE; static final String DATA_FOLDER_KEY; static final String OUTPUT_FILE_KEY; static final String OUTPUT_ENCODING_KEY; static final String PREFIX_KEY; static final String POSTFIX_KEY; static final String FACTORY_KEY; static final String PACKAGES_KEY; static final String ENTITY_PACKAGES_KEY; static final String STATEMENTS_WRITER_KEY; }