I can not generate the table through Hibernate

3

I'm trying to create tables automatically by hibernate, but I'm not succeeding.

The hibernate libraries are correct. the mysql connector is referenced as well as the hibernate libraries. I believe the problem is in the GeraTabela class.

I'm making the code available for help.

package br.com.teste;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class GeraTabela {
    public static void main(String[] args) {
        Configuration conf = new AnnotationConfiguration();
        conf.configure();
        SchemaExport se = new SchemaExport(conf);
        se.create(true, true);
    }
}

Employee Employee

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;

    }

}

Position entity

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;

    }

}

class

package br.com.teste.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {


    private static SessionFactory sessionFactory;

    static{
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).buildServiceRegistry();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);

        }
    }
    public static Session getSession(){
        return sessionFactory.openSession();

    }

}

hibernate

<?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:3306/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>

To work you have to right-click on the GenerateTab class to run as an application.

    
asked by anonymous 12.08.2014 / 01:08

1 answer

2

The GeraTabela class is using a Configuration that it creates when it should use the object created in HibernateUtil . Rather than exposing this object, you can create a method for creating the base:

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static Configuration cfg;

    static {
        // ...
    }

    public static void criarDatabase() {
        SchemaExport exporter = new SchemaExport(cfg);
        exporter.create(true, true);
    }
}

Then just call HibernateUtil.criarDatabase() on your main.

    
12.08.2014 / 15:47