Connection to database and mapping

Asked

Viewed 79 times

0

@Techies

I re-asked the question with an appropriate title, based on the initial doubt in:

What to do after mapping?

Hello.

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

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

  1. Even so, I need to map my table to another class using Entity?

  2. What about ddl commands that he talks about in the comment lines, does that mean it’s a dynamic process? Will you always drop and recreate? Sorry, I’m trying to understand. That’s why so many questions.

Hibernateutil: (Put this Class inside the useful package). The question is:

  1. I create the useful package? Where do I find the useful one? I also didn’t understand, but once apologies.

    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 speaks 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.

Back to the question above: will always be DDL?

A hug.

1 answer

3


Let’s go in pieces:

1: Even so, I need to map my table in another class using Entity?

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


2: What about ddl commands that he talks about in the comment lines, does it mean that it will be a dynamic process? He will always drop and recreate?

R: Also no, you use the create to create the tables in the database, create drops all tables and recreates them again. After using create for the first time you can exchange for update or validate. Of a researched on what each one does that you will understand better


3: I create the useful package? Where I find the useful?

R: You can put this class in any package, but it is recommended to leave it in the package util, you will be able to use this class in all your projects, I recommend that of a survey on utility classes as well.


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

  • The table is already created, I will search about update and validate, since create does not matter, at least now.

  • What kind of project should I create for java web? Would jpa? I’m asking because the projects have their structure and if I’m in any doubt, it helps to pass on details.

  • It depends, if you go with Maven, create a Maven project. If you want a normal web create a Dynamic web project

  • in Hibernate.cfg, in line <Property name="hbm2ddl.auto">create</Property> I changed to <Property name="hbm2ddl.auto">update</Property> .

  • All right, if you want to study and learn a little bit of everything, here’s a free course: https://www.youtube.com/watch?v=9PGp1T242hA&list=PL_GwGUsBlNyfI0W3ggfffhBdJUqB4981Z

Browser other questions tagged

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