I cannot generate the table by Hibernate

Asked

Viewed 1,013 times

3

I’m trying to create tables automatically by Hibernate, but I’m not succeeding.

The Iberian libraries are correct. mysql connector is referenced as well as Hibernate libraries. I believe the problem is in the Geratable class.

I am providing the code 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 entity

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;

    }

}

office 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 has to right click the mouse in the Gerartable class to run as application.

1 answer

2

The Geratable class is using a Configuration which she herself creates, when she should use the object created in HibernateUtil. Better 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() in its main.

Browser other questions tagged

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