How to create tables when starting Sisitema with JSF and JPA?

Asked

Viewed 210 times

3

Guys, how do I make sure that when the system starts the tables you are created automatically? The way my system is, tables are only created when some kind of access to the bank is done. For example on the login screen just press the login button that the tables are created. My intention is that they are created when starting the system.

I am using JPA + JSF + Mysql

  • Which Server is Using?

  • You’re using hibernate too?

3 answers

1

In your case I would create a class that implements Servletcontextlistener which is a Listener that checks when the start and end of an application, when implementing this class two superscript methods are created: contextInitialized() and contextDestroyed().

In the method contextInitialized() you start what you want together with the server, example:

@Override
public void contextInitialized(ServletContextEvent sce) {
    emf = Persistence.createEntityManagerFactory(pu);
    System.out.println("Contexto Inicializado");
}

And in the method contextDestroyed() you put everything you want to finish together with the server, example:

@Override
public void contextDestroyed(ServletContextEvent sce) {
   emf.close();
}

Finally just register the Listener in the web.xml

<listener>
  <listener-class>com.util.suaclasse</listener-class>
</listener>

0

Set the value of the property "Hibernate.hbm2ddl.auto" as "update".

Thus, any change in JPA entities will be reflected in the database when the server starts the application.

Follow example using Mysql, Hibernate and Wildfly 9 server

<persistence-unit name="myPU" transaction-type="JTA">

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/myDataSource</jta-data-source>

    <properties>
        <property name="javax.persistence.validation.mode" value="none" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
    </properties>

</persistence-unit>

0

In my case I use the wildfly, I did the following:

I set it up in the file standalone.xml the following entries:

In <datasources I added:

<datasource jndi-name="java:jboss/datasources/pokemax" pool-name="pokemax" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost:3306/banco?useTimezone=true&amp;serverTimezone=UTC</connection-url>
                    <driver>mysql</driver>
                    <security>
                        <user-name>user</user-name>
                        <password>senha</password>
                    </security>
                </datasource>

In <drivers:

<driver name="mysql" module="com.mysql">
                        <driver-class>com.mysql.jdbc.Driver</driver-class>
                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                    </driver>

And add the driver of mysql in the directory: /opt/wildfly-10.1.0.Final/modules/system/layers/base/com/mysql/main.

If folders do not exist you can create them normally.

Still in this directory, add a file called module.xml with the following information:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-6.0.4.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>

Ready, just watch out for your settings like database, user, password, mysql driver version, etc.

Browser other questions tagged

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