Connection with database and mapping

0

@Techies

Reframe the question with an appropriate title, based on the initial question at:

What to do after mapping?

Hello.

Following the guidelines of another post, I have some questions, starting with the hibernate.cfg file:

<session-factory>

    <!-- CONFIGURAÇÕES DO BANCO, TROCAR "NOMEDOBANCO" PARA O NOME DO SEU BANCO QUE JÁ FOI CRIADO -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/agenda</property>
    <property name="connection.username">root</property>
    <property name="connection.password">portal</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Dropa e Recria todas as tabelas, mudar para update ou validade após primeira execução -->
    <property name="hbm2ddl.auto">create</property>
    <!--AQUI VOCÊ MAPEIA SUA CLASSE AGENDA.-->
    <mapping class="com.sistema.model.Agenda" />
</session-factory>

In this file the user who helped me talks about mapping the agenda class. The question is:

  • Even though I need to map my table to another class using Entity?

  • And about ddl commands that it speaks in comment lines, does that mean it's a dynamic process? Will you always drop and recreate? Sorry, I'm trying to understand. So much question.

  • HibernateUtil: (Put this class inside the useful package). The question is:

  • I create the package util? Where do I find the util? I also do not understand it, but once I apologize.

    public class HibernateUtil {
    
    private static final SessionFactory sessionFactory = buildSessionFactory();
    
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure();
    
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
    
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;
    
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    

    }

  • And the user talks in test class to generate table:

    public class GeraTabela {
    
        public static void main(String[] args) {
            HibernateUtil.getSessionFactory();
            //TESTE
            HibernateUtil.getSessionFactory().close();
        }
    
    }
    

    If all goes well when you run this class your table will be created in the database.

    I return to the question above: will it always be DDL?

    A hug.

        
    asked by anonymous 01.10.2015 / 13:50

    1 answer

    3

    Let's break it down:

    1: Even though I need to map my table to another class using Entity?

    R: No, because you already mapped it to the file hibernate.cfg

    2: And about ddl commands that it speaks in comment lines, does that mean it will be a dynamic process? Will you always drop and recreate?

    R: Also, you do not use create to create the tables in the database, create drops all tables and rebuilds them. After you have used create for the first time you can switch to update or validate . From a researched on what each one does that you will understand better

    3: I create the package util? Where do I find the utility?

    R: You can put this class in any package, but it is recommended to leave it in the package util , you can use this class in all your projects, utilitarian classes as well.

    And for the last question, the only thing you have to change is the values passed to the property hbm2ddl.auto : CREATE , UPDATE AND VALIDATE . If it is the first time you are going to run the system, the tables will still not be generated, so use create to see if the tables were created and then change to update or validate .     

    01.10.2015 / 14:10