Maven project with Hibernate and Glassfish does not generate database

Asked

Viewed 538 times

4

I have a Maven project and I’m having problems generating the database, I’m using Hibernate and Glassfish 4.

No error is generated, just no data is generated, I’m new with Maven maybe I’m forgetting some additional configuration, but I have no idea what it is, below are my configuration files and the Glassfish log when starting:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.financeiro</groupId>
    <artifactId>Financeiro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <!-- Propiedades do Projeto -->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- Dependências do Projeto -->

    <dependencies>

        <!-- Servlet API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Núcleo do Hibernate -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.8.Final</version>
            <scope>compile</scope>
        </dependency>

        <!-- Implementação de EntityManager da JPA -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.8.Final</version>
            <scope>compile</scope>
        </dependency>

        <!-- Driver JDBC do MySQL -->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
</project>

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Financeiro">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
    </persistence-unit>
</persistence>

Pessoa.class

package com.financeiro.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "PESSOA")
public class Pessoa implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private long id;

    @Column(name = "NOME", length = 80, nullable = false)
    private long nome;

    public long getId() {
        return id;
    }

    public long getNome() {
        return nome;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setNome(long nome) {
        this.nome = nome;
    }

}

Glassfish log:

2015-03-10T09:12:17.929-0300|Informações: Running GlassFish Version: GlassFish Server Open Source Edition  4.1  (build 13)
2015-03-10T09:12:17.932-0300|Informações: Server log file is using Formatter class: com.sun.enterprise.server.logging.ODLLogFormatter
2015-03-10T09:12:18.406-0300|Informações: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
2015-03-10T09:12:18.408-0300|Informações: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
2015-03-10T09:12:18.423-0300|Informações: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
2015-03-10T09:12:18.523-0300|Informações: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
2015-03-10T09:12:18.665-0300|Informações: Authorization Service has successfully initialized.
2015-03-10T09:12:18.779-0300|Informações: Grizzly Framework 2.3.15 started in: 37ms - bound to [/0.0.0.0:8080]
2015-03-10T09:12:18.831-0300|Informações: Grizzly Framework 2.3.15 started in: 5ms - bound to [/0.0.0.0:8181]
2015-03-10T09:12:18.870-0300|Informações: Grizzly Framework 2.3.15 started in: 5ms - bound to [/0.0.0.0:4848]
2015-03-10T09:12:18.968-0300|Informações: Grizzly Framework 2.3.15 started in: 3ms - bound to [/0.0.0.0:3700]
2015-03-10T09:12:21.355-0300|Informações: visiting unvisited references
2015-03-10T09:12:22.348-0300|Informações: Java security manager is disabled.
2015-03-10T09:12:22.348-0300|Informações: Entering Security Startup Service.
2015-03-10T09:12:22.353-0300|Informações: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper.
2015-03-10T09:12:22.412-0300|Informações: Security Service(s) started successfully.
2015-03-10T09:12:22.801-0300|Informações: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080
2015-03-10T09:12:22.817-0300|Informações: Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181
2015-03-10T09:12:22.825-0300|Informações: Created HTTP listener admin-listener on host/port 0.0.0.0:4848
2015-03-10T09:12:22.870-0300|Informações: Created virtual server server
2015-03-10T09:12:22.874-0300|Informações: Created virtual server __asadmin
2015-03-10T09:12:23.278-0300|Informações: Setting JAAS app name glassfish-web
2015-03-10T09:12:23.280-0300|Informações: Virtual server server loaded default web module 
2015-03-10T09:12:24.137-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.235-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.238-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.241-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.244-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.247-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.249-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.252-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.255-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.258-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.261-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.264-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.267-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.270-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.273-0300|Informações: visiting unvisited references
2015-03-10T09:12:24.487-0300|Advertência: The web application [unknown] registered the JDBC driver [com.mysql.fabric.jdbc.FabricMySQLDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
2015-03-10T09:12:24.678-0300|Informações: Loading application [Financeiro] at [/Financeiro]
2015-03-10T09:12:24.681-0300|Informações: Loading application Financeiro done in 6.164 ms
2015-03-10T09:12:24.687-0300|Informações: GlassFish Server Open Source Edition  4.1  (13) startup time : Felix (3.730ms), startup services(6.923ms), total(10.653ms)
2015-03-10T09:12:24.844-0300|Informações: Grizzly Framework 2.3.15 started in: 1ms - bound to [/0.0.0.0:7676]
2015-03-10T09:12:24.936-0300|Informações: HV000001: Hibernate Validator 5.0.0.Final
2015-03-10T09:12:25.722-0300|Informações: JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://pereira:8686/jndi/rmi://pereira:8686/jmxrmi
2015-03-10T09:12:28.197-0300|Informações: Initiating Jersey application, version Jersey: 2.10.4 2014-08-08 15:09:00...
2015-03-10T09:12:29.040-0300|Informações: Listening to REST requests at context: /management/domain.
2015-03-10T09:12:29.313-0300|Informações: Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@e392108 as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@6b9c9961.
2015-03-10T09:12:29.754-0300|Informações: visiting unvisited references
2015-03-10T09:12:30.298-0300|Informações: visiting unvisited references
2015-03-10T09:12:30.316-0300|Informações: visiting unvisited references
2015-03-10T09:12:30.318-0300|Informações: visiting unvisited references
2015-03-10T09:12:32.397-0300|Informações: Inicializando Mojarra 2.2.7 ( 20140610-1547 https://svn.java.net/svn/mojarra~svn/tags/2.2.7@13362) para o contexto ''
2015-03-10T09:12:33.951-0300|Informações: Loading application [__admingui] at [/]
2015-03-10T09:12:33.953-0300|Informações: Loading application __admingui done in 4.640 ms

OBS: glassfish correctly configured the Connection pool

  • Fabio, your application is not generating any log or you picked up the incorrect log. I suspect that JPA didn’t even initialize the context. Check that the application log is correctly configured and that the log is being written to another file. If you get this information, edit your question with the data so we can analyze the problem. Hug!

  • OK as soon as possible I see this with more caution and put here

  • Look I versified all the log files generated by glassfish in understand all are equal to the posted, there is no Hibernate boot, I have no idea what might be causing this is the first time I’ve worked with Maven I’ve ever used the Dynamic Web Project and the way I’ve set it up has always worked. I’d have something I should try ?

  • Then you should not have the application log. Set up a file log4.xml in your application so that it manages the logs correctly. At this time, use the DEBUG log level to facilitate the investigation of the problem. The documentation on how to configure Log4j is in the link: http://logging.apache.org/log4j/2.x/manual/configuration.html

  • @1fabiopereira, already solved the problem of the question?

2 answers

1

Use the property

  <property name="hibernate.hbm2ddl.auto" value="update" />

instead of

  <property name="hibernate.hbm2ddl.auto" value="create-drop" />

1

Well, your persistence.xml file doesn’t have the type of transaction your application will use (local Resource or data source), the declaration of your entity classes, and the database properties you’re using, so you don’t create the database. Like you said you do

Example:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="exemploPU" > 
    <jta-data-source>java:/BancoDataSources</jta-data-source> <!--configuração do datasource e conction pool -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>br.com.controle.model.Pessoa</class> <!-- Entity -->

        <properties>
            <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>

</persistence>

Browser other questions tagged

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