Creating automatic tables by Hibernate

4

Good afternoon,

I'm trying to create the tables automatically, being that I think I should be missing something.

Can anyone help me?

this is my line of code

Cargo.java

package br.com.teste;

import java.io.Serializable;

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


@Entity
@Table
public class Cargo implements Serializable {

    private int id;

    private String descricao;

    public String getDescricao() {    
        return descricao;    
    }

    public void setDescricao(String descricao) {    
        this.descricao = descricao;    
    }

    @Id
    @GeneratedValue
    public int getId() {    
        return id;    
    }

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

Official.java

package br.com.teste;

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

@Entity
@Table
public class Funcionario {

    private int id;

    private String nome;

    private String email;

    private Cargo cargo = new Cargo();

    public Cargo getCargo() {    
        return cargo;
    }

    public void setCargo(Cargo cargo) {    
        this.cargo = cargo;    
    }

    public String getEmail() {    
        return email;    
    }

    public void setEmail(String email) {    
        this.email = email;    
    }

    @Id
    @GeneratedValue
    public int getId() {    
        return id;    
    }

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

    public String getNome() {    
        return nome;    
    }

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

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:/localhost/teste</property>
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password">123</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect </property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>

        <mapping class="br.com.teste.Cargo"/>

        <mapping class="br.com.teste.Funcionario"/>

        </session-factory>
</hibernate-configuration>

console (logs)

Ago 11, 2014 5:20:38 PM org.apache.catalina.core.AprLifecycleListener init
INFORMAÇÕES: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Ago 11, 2014 5:20:38 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
ADVERTÊNCIA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Projeto' did not find a matching property.
Ago 11, 2014 5:20:38 PM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["http-bio-8080"]
Ago 11, 2014 5:20:38 PM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["ajp-bio-8009"]
Ago 11, 2014 5:20:38 PM org.apache.catalina.startup.Catalina load
INFORMAÇÕES: Initialization processed in 587 ms
Ago 11, 2014 5:20:38 PM org.apache.catalina.core.StandardService startInternal
INFORMAÇÕES: Starting service Catalina
Ago 11, 2014 5:20:38 PM org.apache.catalina.core.StandardEngine startInternal
INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/7.0.55
Ago 11, 2014 5:20:40 PM org.apache.catalina.core.StandardContext addApplicationListener
INFORMAÇÕES: The listener "com.sun.faces.config.ConfigureListener" is already configured for this context. The duplicate definition has been ignored.
Ago 11, 2014 5:20:40 PM com.sun.faces.config.ConfigureListener contextInitialized
INFORMAÇÕES: Inicializando Mojarra 2.1.7 (SNAPSHOT 20120206) para o contexto '/Projeto'
Ago 11, 2014 5:20:40 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFORMAÇÕES: JSF1048: Anotações PostConstruct/PreDestroy presentes.  Os métodos ManagedBeans marcados com essas anotações informarão as anotações processadas.
Ago 11, 2014 5:20:40 PM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["http-bio-8080"]
Ago 11, 2014 5:20:40 PM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["ajp-bio-8009"]
Ago 11, 2014 5:20:40 PM org.apache.catalina.startup.Catalina start
    
asked by anonymous 11.08.2014 / 21:28

1 answer

1

Your tables have not yet been created and / or you still have not gotten any error / success because Hibernate has not yet been triggered in any way by your application (I'm assuming you're in a web container instead of an application server ).

If you choose to use the JPA specification classes (persistence.xml) rather than Hibernate (hibernate.cfg.xml), you can do:

Persistence.createEntityManagerFactory("nomeDaSuaEntityManager"); // no caso do xml, o nome da entityManager é "local".

To avoid any surprise, I am putting an example of persistence.xml that works on a tomcat:

<persistence
    version="2.0"
    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">

    <persistence-unit name="local" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/teste" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="123" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

</persistence>
    
05.09.2014 / 04:01