List of usage examples for org.hibernate.tool.hbm2ddl SchemaExport create
public void create(EnumSet<TargetType> targetTypes, Metadata metadata)
From source file:org.giavacms.base.controller.ImportController.java
License:Open Source License
public static void main(String[] args) { Configuration cfg2 = new Configuration(); SchemaExport schemaExport2 = new SchemaExport(cfg2); // schemaExport2.setImportFile("/import-2.sql"); schemaExport2.create(false, true); }
From source file:org.infoglue.calendar.controllers.SchemaExportController.java
License:Open Source License
public void exportTables() throws HibernateException { Configuration cfg = new Configuration().addClass(Calendar.class).addClass(Location.class) .addClass(Category.class).addClass(Participant.class).addClass(Resource.class) .addClass(Event.class); cfg.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.MySQLDialect"); SchemaExport schemaExport = new SchemaExport(cfg); schemaExport.setOutputFile("calendar.sql"); schemaExport.create(true, true); }
From source file:org.jboss.test.hibernate.model.v1.PersonBean.java
License:Open Source License
public void init() throws HibernateException { Configuration cfg = new Configuration().configure(); SchemaExport se = new SchemaExport(cfg); se.create(true, true); }
From source file:org.jbpm.ant.JbpmSchemaTask.java
License:Open Source License
public void execute() throws BuildException { if (actions == null) { // default action is create actions = "create"; }//from ww w . j a v a 2s .co m // we need a configuration Configuration configuration = null; // if there is no jbpm nor hibernate configuration specified if ((jbpmCfg == null) && (hibernateCfg == null)) { // search for the default jbpm.cfg.xml URL defaultJbpmCfgUrl = getClass().getClassLoader().getResource("jbpm.cfg.xml"); if (defaultJbpmCfgUrl != null) { jbpmCfg = "jbpm.cfg.xml"; // if still not found, search for the default hibernate.cfg.xml } else { URL defaultHibernateCfgUrl = getClass().getClassLoader().getResource("hibernate.cfg.xml"); if (defaultHibernateCfgUrl != null) { hibernateCfg = "hibernate.cfg.xml"; } } } // first see if the jbpm cfg is specified cause that implies a hibernate configuration if (jbpmCfg != null) { log("using jbpm configuration " + jbpmCfg); JbpmConfiguration jbpmConfiguration = AntHelper.getJbpmConfiguration(jbpmCfg); JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); try { DbPersistenceServiceFactory dbPersistenceServiceFactory = (DbPersistenceServiceFactory) jbpmConfiguration .getServiceFactory(Services.SERVICENAME_PERSISTENCE); configuration = dbPersistenceServiceFactory.getConfiguration(); } finally { jbpmContext.close(); } // if there is no jbpm.cfg.xml specified, check if there is a hibernate.cfg.xml specified } else if (hibernateCfg != null) { log("using hibernate configuration " + hibernateCfg); configuration = AntHelper.getConfiguration(hibernateCfg, hibernateProperties); // no hibernate configuration specified } else { throw new BuildException("couldn't create schema. no jbpm nor hibernate configuration specified."); } JbpmSchema jbpmSchema = new JbpmSchema(configuration); SchemaExport schemaExport = new SchemaExport(configuration); if (output != null) schemaExport.setOutputFile(output); if (delimiter != null) schemaExport.setDelimiter(delimiter); StringTokenizer tokenizer = new StringTokenizer(actions, ","); while (tokenizer.hasMoreTokens()) { String action = tokenizer.nextToken(); if ("drop".equalsIgnoreCase(action)) { schemaExport.drop(!quiet, !text); } else if ("create".equalsIgnoreCase(action)) { schemaExport.create(!quiet, !text); } else if ("clean".equalsIgnoreCase(action)) { jbpmSchema.cleanSchema(); } } }
From source file:org.jooq.example.jpa.JPAExample.java
License:Apache License
@SuppressWarnings("serial") public static void main(String[] args) throws Exception { Connection connection = null; EntityManagerFactory emf = null;/*from w ww. j a va 2s . c o m*/ EntityManager em = null; try { // Bootstrapping JDBC: Class.forName("org.h2.Driver"); connection = DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", ""); final Connection c = connection; // Creating an in-memory H2 database from our entities MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder() .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect") .applySetting("javax.persistence.schema-generation-connection", connection) .applySetting("javax.persistence.create-database-schemas", true) // [#5607] JPADatabase causes warnings - This prevents // them .applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() { @SuppressWarnings("rawtypes") @Override public boolean isUnwrappableAs(Class unwrapType) { return false; } @Override public <T> T unwrap(Class<T> unwrapType) { return null; } @Override public Connection getConnection() { return c; } @Override public void closeConnection(Connection conn) throws SQLException { } @Override public boolean supportsAggressiveRelease() { return true; } }).build()); metadata.addAnnotatedClass(Actor.class); metadata.addAnnotatedClass(Film.class); metadata.addAnnotatedClass(Language.class); SchemaExport export = new SchemaExport(); export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata()); // Setting up an EntityManager using Spring (much easier than out-of-the-box Hibernate) LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect()); bean.setDataSource(new SingleConnectionDataSource(connection, true)); bean.setPackagesToScan("org.jooq.example.jpa.entity"); bean.setJpaVendorAdapter(adapter); bean.setPersistenceUnitName("test"); bean.setPersistenceProviderClass(HibernatePersistenceProvider.class); bean.afterPropertiesSet(); emf = bean.getObject(); em = emf.createEntityManager(); final EntityManager e = em; // Run some Hibernate / jOOQ logic inside of a transaction em.getTransaction().begin(); run(em, DSL.using(new DefaultConfiguration().set(connection).set(new DefaultExecuteListener() { @Override public void start(ExecuteContext ctx) { // Flush all changes from the EntityManager to the database for them to be visible in jOOQ e.flush(); super.start(ctx); } }))); em.getTransaction().commit(); } finally { if (em != null) em.close(); if (emf != null) emf.close(); if (connection != null) connection.close(); } }
From source file:org.jooq.example.jpa.Setup.java
License:Apache License
static void run(BiConsumer<EntityManager, DSLContext> consumer) throws Exception { Connection connection = null; EntityManagerFactory emf = null;//from w w w. j a v a 2s .c om EntityManager em = null; try { // Bootstrapping JDBC: Class.forName("org.h2.Driver"); connection = new LoggingConnection( DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", "")); final Connection c = connection; // Creating an in-memory H2 database from our entities MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder() .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect") .applySetting("javax.persistence.schema-generation-connection", connection) .applySetting("javax.persistence.create-database-schemas", true) // [#5607] JPADatabase causes warnings - This prevents // them .applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() { @SuppressWarnings("rawtypes") @Override public boolean isUnwrappableAs(Class unwrapType) { return false; } @Override public <T> T unwrap(Class<T> unwrapType) { return null; } @Override public Connection getConnection() { return c; } @Override public void closeConnection(Connection conn) throws SQLException { } @Override public boolean supportsAggressiveRelease() { return true; } }).build()); metadata.addAnnotatedClass(Actor.class); metadata.addAnnotatedClass(Film.class); metadata.addAnnotatedClass(Language.class); SchemaExport export = new SchemaExport(); export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata()); // Setting up an EntityManager using Spring (much easier than out-of-the-box Hibernate) LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect()); bean.setDataSource(new SingleConnectionDataSource(connection, true)); bean.setPackagesToScan("org.jooq.example.jpa.entity"); bean.setJpaVendorAdapter(adapter); bean.setPersistenceUnitName("test"); bean.setPersistenceProviderClass(HibernatePersistenceProvider.class); bean.afterPropertiesSet(); emf = bean.getObject(); em = emf.createEntityManager(); final EntityManager e = em; // Run some Hibernate / jOOQ logic inside of a transaction em.getTransaction().begin(); data(em); consumer.accept(em, DSL.using(new DefaultConfiguration().set(connection).set(new DefaultExecuteListener() { @Override public void start(ExecuteContext ctx) { // Flush all changes from the EntityManager to the database for them to be visible in jOOQ e.flush(); super.start(ctx); } }))); em.getTransaction().commit(); } finally { if (em != null) em.close(); if (emf != null) emf.close(); if (connection != null) connection.close(); } }
From source file:org.jooq.meta.extensions.jpa.JPADatabase.java
License:Apache License
@Override protected DSLContext create0() { if (connection == null) { String packages = getProperties().getProperty("packages"); if (isBlank(packages)) { packages = ""; log.warn("No packages defined", "It is highly recommended that you provide explicit packages to scan"); }//from w w w . j av a 2s . c om // [#9058] Properties use camelCase notation. boolean useAttributeConverters = Boolean.valueOf(getProperties().getProperty("useAttributeConverters", getProperties().getProperty("use-attribute-converters", "true"))); String unqualifiedSchema = getProperties().getProperty("unqualifiedSchema", "none").toLowerCase(); publicIsDefault = "none".equals(unqualifiedSchema); try { Properties info = new Properties(); info.put("user", "sa"); info.put("password", ""); connection = new org.h2.Driver().connect("jdbc:h2:mem:jooq-meta-extensions-" + UUID.randomUUID(), info); // [#6709] Apply default settings first, then allow custom overrides Map<String, Object> settings = new LinkedHashMap<>(); settings.put("hibernate.dialect", HIBERNATE_DIALECT); settings.put("javax.persistence.schema-generation-connection", connection); settings.put("javax.persistence.create-database-schemas", true); // [#5607] JPADatabase causes warnings - This prevents them settings.put(AvailableSettings.CONNECTION_PROVIDER, connectionProvider()); for (Entry<Object, Object> entry : getProperties().entrySet()) { String key = "" + entry.getKey(); if (key.startsWith("hibernate.") || key.startsWith("javax.persistence.")) userSettings.put(key, entry.getValue()); } settings.putAll(userSettings); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); builder.applySettings(settings); MetadataSources metadata = new MetadataSources(builder.applySettings(settings).build()); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider( true); scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); // [#5845] Use the correct ClassLoader to load the jpa entity classes defined in the user project ClassLoader cl = Thread.currentThread().getContextClassLoader(); for (String pkg : packages.split(",")) for (BeanDefinition def : scanner.findCandidateComponents(defaultIfBlank(pkg, "").trim())) metadata.addAnnotatedClass(Class.forName(def.getBeanClassName(), true, cl)); // This seems to be the way to do this in idiomatic Hibernate 5.0 API // See also: http://stackoverflow.com/q/32178041/521799 // SchemaExport export = new SchemaExport((MetadataImplementor) metadata.buildMetadata(), connection); // export.create(true, true); // Hibernate 5.2 broke 5.0 API again. Here's how to do this now: SchemaExport export = new SchemaExport(); export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata()); if (useAttributeConverters) loadAttributeConverters(metadata.getAnnotatedClasses()); } catch (Exception e) { throw new DataAccessException("Error while exporting schema", e); } } return DSL.using(connection); }
From source file:org.jooq.util.jpa.JPADatabase.java
License:Open Source License
@Override protected DSLContext create0() { if (connection == null) { String packages = getProperties().getProperty("packages"); if (isBlank(packages)) { packages = ""; log.warn("No packages defined", "It is highly recommended that you provide explicit packages to scan"); }// ww w . j a v a2 s .c o m try { connection = DriverManager.getConnection("jdbc:h2:mem:jooq-meta-extensions", "sa", ""); final Configuration configuration = new Configuration().setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider( true); scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); for (String pkg : packages.split(",")) { for (BeanDefinition def : scanner.findCandidateComponents(defaultIfBlank(pkg, "").trim())) { configuration.addAnnotatedClass(Class.forName(def.getBeanClassName())); } } configuration.generateSchemaCreationScript(Dialect.getDialect(configuration.getProperties())); SchemaExport export = new SchemaExport(configuration, connection); export.create(true, true); } catch (Exception e) { throw new DataAccessException("Error while exporting schema", e); } } return DSL.using(connection); }
From source file:org.jpos.ee.DB.java
License:Open Source License
/** * Creates database schema// w w w . j ava 2s .c o m * * @param outputFile optional output file (may be null) * @param create true to actually issue the create statements */ public void createSchema(String outputFile, boolean create) throws HibernateException { SchemaExport export = new SchemaExport(getHibernateAccessService().getConfiguration()); if (outputFile != null) { export.setOutputFile(outputFile); export.setDelimiter(";"); } export.create(true, create); }
From source file:org.jpos.gl.tools.Import.java
License:Open Source License
private void createSchema() throws HibernateException { SchemaExport export = new SchemaExport(cfg); // export.setOutputFile ("/tmp/schema.sql"); // export.setDelimiter (";"); export.create(true, true); // don't create tables }