How to create Queries to generate a JPA SCHEMA

Asked

Viewed 62 times

0

I am trying to create a query to create a schema in sql automatically; as soon as opening the program it executes: createQuery("CREATE NEW SCHEMA IF NOT EXISTS");

My project is like this:

-Class Entitymanagersource

public class EntityManagerSource {

private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenciaDAO");

@Produces @RequestScoped
public static EntityManager getEntityManager(){
    System.out.println("Banco de Dados: Conectado");
    return emf.createEntityManager();
}

-PERSISTENCE.XML class

<persistence-unit name="PersistenciaDAO" transaction-type="RESOURCE_LOCAL">
 <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>br.edu.ifma.ticketif.model.entity.database.Aluno</class>
    <class>br.edu.ifma.ticketif.model.entity.Usuario</class>
<properties>

  <!--  propriedades do servidor -->
    <property name="javax.persistence.jdbc.url"
              value="jdbc:mysql://localhost:3306/BancoDeDados?useTimezone=true&amp;serverTimezone=UTC&amp;useSSL=false"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.password" value="admin"/>
  <property name="javax.persistence.schema-generation.database.action" value="create"/>
  <property name="hbm2ddl.auto" value="create"/>

  <!--  propriedades da hibernate -->
  <property name="hibernate.dialect"
            value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
  <property name="hibernate.show_sql" value="true"/>
  <property name="hibernate.format_sql" value="true"/>
  <property name="hibernate.use_sql_comments" value="false" />
  <property name="hibernate.jdbc.wrap_result_sets" value="false" />
  <property name="hibernate.hibernate.cache.use_query_cache" value="true" />

  <!--  Atualizações do banco de dados -->
  <property name="hibernate.hbm2ddl.auto" value="update"/>

</properties>

1 answer

0


I was only able to change Persistence.xml

Using the createDatabaseIfNotExist=true command

Stayed like this:

   <property name="javax.persistence.jdbc.url"
              value="jdbc:mysql://localhost:3306/BancoDeDados
              ?useTimezone=true&amp;
              serverTimezone=UTC&amp;
              useSSL=false&amp;
              createDatabaseIfNotExist=true"/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.