Hibernate is not creating the tables

2

I'm new to Hibernate, because of the little I've learned, I believe I did everything right, that is, I downloaded the dependencies, I wrote down the classes, etc. Below is my example class:

package br.com.evolutionary.modelo;

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

@Entity
public class Pokemon {

    @Id
    @GeneratedValue
    private Long id;

    @Column
    private String nome;

    public Long getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

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

}

Now follows my persistence.xml file:

<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="evolutionary"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/evolutionary" />
            <property name="hibernate.connection.driver" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

My pom.xml file:

<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>br.com</groupId>
    <artifactId>evolutionary</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Next, when starting tomcat, the entities are not created, I do not see the hibernate log. So my questions are:
Need to set up something on tomcat?
Is Hibernate Started Any Other Way?
Somebody help me, please?

Note: The above files have been changed based on the answers already obtained and by questions.

    
asked by anonymous 12.05.2016 / 01:18

6 answers

0

The problem can be solved when I made settings inside the wildfly like this:

<datasource jndi-name="java:jboss/datasources/pokemax" pool-name="pokemax" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost:3306/pokemax?useTimezone=true&amp;serverTimezone=UTC</connection-url>
                    <driver>mysql</driver>
                    <security>
                        <user-name>pokemax</user-name>
                        <password>pokemax</password>
                    </security>
                </datasource>
                <drivers>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                    <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>
    
29.09.2016 / 19:14
4

There are two problems with your XML.

1- The hibernate.hbm2ddl.auto property is missing, where it is being commented out.

2 - The mapped classes in Xml are missing.

Add such tags to your Xml to solve the problem.

<!--  atualiza o banco, gera as tabelas se for preciso -->
<property name="hibernate.hbm2ddl.auto" value="update" />

<!-- entidade mapeada -->
<class>br.com.sales.model.Empresa</class>
  

XML example:

<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="model" transaction-type="RESOURCE_LOCAL">

   <!-- provedor/implementacao do JPA -->
   <provider>org.hibernate.ejb.HibernatePersistence</provider>

   <!-- entidade mapeada -->
   <class>br.com.sales.model.Empresa</class>
   <class>br.com.sales.model.Cliente</class>
   <class>br.com.sales.model.ProdutosVO</class>
   <class>br.com.sales.model.Logger</class>

  <properties>
  <!-- dados da conexao -->
  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
  <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sales" />
  <property name="hibernate.connection.username" value="root" />
  <property name="hibernate.connection.password" value="connect123" />

   <!--  propriedades do hibernate -->
  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
  <property name="hibernate.show_sql" value="false" />
  <property name="hibernate.format_sql" value="false" />

  <!--  atualiza o banco, gera as tabelas se for preciso -->
  <property name="hibernate.hbm2ddl.auto" value="update" />

   </properties>
 </persistence-unit>
</persistence>

In the controller, you just need to point to the Xml:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("model");
    
12.05.2016 / 15:54
2

I use eclipselink and after the persistence-unit I have to declare the classes that are Entity. Here is an example:

  <persistence-unit name="clientes" transaction-type="RESOURCE_LOCAL">
       <class>JPA.ClientePOJO</class>
       <class>JPA.EmprestimoPOJO</class>
    
17.05.2016 / 01:50
1

Uncomment the line
property name="hibernate.hbm2ddl.auto" value="create"

    
12.05.2016 / 14:42
1

Good morning, add the following property in your persistence.xml:

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

With the update option it will always keep the database updated every time you run the application.

Here is an example of my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit name="NewPersistenceUnit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Ramon"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto"" value="update"/>
    </properties>
</persistence-unit>

    
12.05.2016 / 14:41
1

For your entities to be detected automatically, the persistence.xml must be in the same classpath as the classes annotated with @Entity, you must also set the element as false:

<persistence-unit name="evolutionary" transaction-type="RESOURCE_LOCAL">
    <exclude-unlisted-classes>false</exclude-unlisted-classes>    

And then set the property:

<properties>
    <property name="hibernate.archive.autodetection" value="class, hbm" />
    
17.05.2016 / 03:36