Hibernate is not creating the tables

Asked

Viewed 2,011 times

2

I am new to the subject Hibernate, for the little I have learned, I believe I did everything right, ie, I lowered the dependencies, I wrote down the classes, etc. Below follows my class example:

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 follow 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, entities are not created, I do not see Hibernate log. So my questions are:
You need to configure something in Tomcat?
The Hibernate is started in another way?
Somebody help me please?

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

  • Did you correctly annotate all the classes that will be persisted? ( Entity, Id, Collumn, etc.. )

  • Post the Log it generates, otherwise it is difficult to help you.

  • @Sérgiomucciaccia I will edit my question and add the code.

  • @Geferson will edit the question and add the output.

  • Class mapping is correct. the persistence file, leave it as update instead of create. Create deletes the bank every time it runs and creates again. update just updates, comments this generateDdl tag, the log I believe has more stuff, you posted what is in the log tab of Tomcat?

  • @Douglas Your problem has been solved?

  • @Matheus in part yes. I’ll make some more adjustments yet.

  • @Douglas, I’ve tried to add your entities in tags <class></class>.?

  • I just experienced the same problem. A possible solution is in the git repository: https://github.com/plinio352/WebHibernate.git

Show 4 more comments

6 answers

4

There are two problems in your XML.

1- The property is missing Hibernate.hbm2ddl.auto, where is being presented commented.

2-Classes mapped in Xml are missing.

Add such tag in 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>

Example of XML:

<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, just point to Xml:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("model");
  • My Hibernate automatically detects annotated tables. It still doesn’t work :/

  • @Douglas Which bank?

  • Mysql. @Matheus

  • @Douglas Tried using sample XML?

  • @Douglas If I’m not mistaken, you’re using the wrong libs when creating the connection. In your case, you are already using javax.persistence in my Hibernate.Connection.

  • I didn’t use your whole xml no, I just checked referring to mine. As I correct the issue of libs?

  • @Just change the references javax.persistence.jdbc to Hibernate.Connection as shown in the example. Probably the libs from Hibernate you already have on the classpath. (Hibernate3 , Hibernate-JPA-2 ).

  • I’ve changed and nothing.

  • I added my pom.xml for further clarification because so far nothing.

  • Another point is: What is the exact moment of Hibernate create the tables? When climbing the application server?

  • @Douglas No, it will only create the tables when the application tries to insert a data into the database or perform a select of the mapped class.

Show 6 more comments

2

I use eclipselink and after persistence-Unit I have to declare the classes that are Entity. Follow below example:

  <persistence-unit name="clientes" transaction-type="RESOURCE_LOCAL">
       <class>JPA.ClientePOJO</class>
       <class>JPA.EmprestimoPOJO</class>

1

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

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

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

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

  • But in my case the bank has not yet been created, the update server for banks already created, no?

  • @Douglas Yes, the bank has to be created.

  • @Matheus to be clear, the bank is already created, Hibernate is not creating the tables, etc. This is my problem.

  • @Douglas Amorim’s answer solves your problem.

  • I still can’t @Matheus, know if Hibernate generates log?

1

Remove the comment from the line
Property name="Hibernate.hbm2ddl.auto" value="create"

1

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

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

And then define the property:

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

0


The problem can be solved when I made settings inside the wildfly getting 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>

Browser other questions tagged

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