0

A long time ago, I did that with a code like that:

Configuration config = new Configuration();

Properties props = new Properties();
FileInputStream fos = = new FileInputStream( file_name );
props.load(fos);
fos.close();
config.setProperties(props);

config.addAnnotatedClass(...);

Connection conn = DriverManager.getConnection(url,usuario,senha);
SchemaExport schema = new SchemaExport();
schema.create(true, true);

But now, if I try use this code, I got a compilation error. Seeing the javadoc for SchemaExport, I notice a lot of changes in the methods used on this example.

Hpw I could do that now?

update

based on the suggested link, I implemented the method this way:

  public void criarTabelas(String server, String user, String pass) throws Exception {
    StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().applySetting("hibernate.hbm2ddl.auto", "create").applySetting("hibernate.dialect", dialect).applySetting("hibernate.id.new_generator_mappings", "true").build();

    MetadataSources sources = new MetadataSources(standardRegistry);
    for(Class<?> entity : lista_entidades())
      sources.addAnnotatedClass(entity);

    MetadataImplementor metadata = (MetadataImplementor) sources.getMetadataBuilder().build();

    SchemaExport export = new SchemaExport();
    export.create(EnumSet.of(TargetType.DATABASE), metadata);
  }

  private List<Class<?>> lista_entidades() throws Exception {
      List<Class<?>> lista = new ArrayList<Class<?>>();

      ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
      scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
      for (BeanDefinition bd : scanner.findCandidateComponents("org.loja.model"))
        lista.add(Class.forName(bd.getBeanClassName()));

      return lista;
    }

Now I need a way to establish a jdbc connection and associate to the SchemaExport.

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
  • 1
    Does this answer your question? [Replacing SchemaExport(Configuration) in Hibernate 5](https://stackoverflow.com/questions/47432115/replacing-schemaexportconfiguration-in-hibernate-5) – Simon Martinelli Apr 21 '20 at 15:42
  • Almost, but still have one question. In the suggested answer, the constructor for SchemaExport gets 1 argument (MetadataImplementor). But in the javaDoc, it's listed only one constructor, with no arguments. SchemaExport have a method create that accepts this MetadataImplementor argumento, alongside `EnumSet targetTypes`. What I should use for this argument? – Kleber Mota Apr 21 '20 at 15:55

1 Answers1

0

I solve this issue with this code:

  public void criarTabelas(String server, String user, String pass) throws Exception {
    Connection conn = DriverManager.getConnection(url_prefix+server+"/"+url_suffix, user, pass);
    StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
    .applySetting("hibernate.hbm2ddl.auto", "create")
    .applySetting("hibernate.dialect", dialect)
    .applySetting("hibernate.id.new_generator_mappings", "true")
    .applySetting("javax.persistence.schema-generation-connection", conn)
    .build();

    MetadataSources sources = new MetadataSources(standardRegistry);
    for(Class<?> entity : lista_entidades())
      sources.addAnnotatedClass(entity);

    MetadataImplementor metadata = (MetadataImplementor) sources.getMetadataBuilder().build();

    SchemaExport export = new SchemaExport();
    export.create(EnumSet.of(TargetType.DATABASE), metadata);
    conn.close();
  }

  private List<Class<?>> lista_entidades() throws Exception {
      List<Class<?>> lista = new ArrayList<Class<?>>();

      ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
      scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
      for (BeanDefinition bd : scanner.findCandidateComponents("org.loja.model"))
        lista.add(Class.forName(bd.getBeanClassName()));

      System.out.println("lista: "+lista);
      return lista;
    }
Kleber Mota
  • 8,521
  • 31
  • 94
  • 188