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

3

Well, how do I get the system started when the tables are created automatically? The way my system is, tables are only created when some kind of bank access is done. For example in the login screen just press the button log that the tables are created. My intention is that they be created at system startup.

I'm using JPA + JSF + MySql

    
asked by anonymous 11.10.2016 / 15:11

3 answers

1

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

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

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

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

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

Finally, just register Listener in web.xml

<listener>
  <listener-class>com.util.suaclasse</listener-class>
</listener>
    
11.10.2016 / 21:54
0

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

I configured the following entries in the standalone.xml file :

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 mysql driver to 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.

In this directory, add a file named 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>

Okay, just look at your settings like bank, user, password, mysql driver version, etc.

    
11.10.2016 / 21:28
0

Set the value of the " hibernate.hbm2ddl.auto " property to " update ".

In this way, any changes to 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>

    
12.10.2016 / 19:22