0
@Techies
I re-asked the question with an appropriate title, based on the initial doubt in:
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:
Even so, I need to map my table to another class using Entity?
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:
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.
The table is already created, I will search about update and validate, since create does not matter, at least now.
– André Nascimento
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.
– André Nascimento
It depends, if you go with Maven, create a Maven project. If you want a normal web create a Dynamic web project
– DiegoAugusto
in Hibernate.cfg, in line <Property name="hbm2ddl.auto">create</Property> I changed to <Property name="hbm2ddl.auto">update</Property> .
– André Nascimento
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
– DiegoAugusto