Analysis and correction of connection test errors using DB using hibernate

3

Class to test bank connection with hibernate.

package br.drogaria.main;

public class HibernatUtilTeste {

    public static void main(String[] args) {

        //abre sessão
        HibernateUtil.getFabricaDeSessao().openSession();

        //fecha sessão
        HibernateUtil.getFabricaDeSessao().close();
    }
}

You are giving the errors:

  
  • configuration can not be resolved HibernatUtil.java / Drugstore / src / main / java / drugstore / util line   17 Java Problem Description Resource Path Location Type
  •   

    The line is:

    ServiceRegistry registro = new StandardServiceRegistryBuilder()
                                  .applySettings(configuration.getProperties()).build();
    
      
  • configuration can not be resolved HibernatUtil.java / Drugstore / src / main / java / drugstore / util line   18 Java Problem Description Resource Path Location Type
  •   

    The line is:

    SessionFactory fabricaDeSessao = configuration.buildSessionFactory(registro);
    
      
  • HibernateUtil can not be resolved HibernatUtilTeste.java / Drugstore / src / main / java / drugstore / main line   7 Java Problem Description Resource Path Location Type
  •   

    The line is:

    HibernateUtil.getFabricaDeSessao().openSession();
    
      
  • The method buildFabricaDeSessao () is undefined for the type HibernatUtil HibernatUtil.java / Drugstore / src / main / java / br / drugstore / util line   9 Java Problem Description Resource Path Location Type
  •   

    The line is:

    private static final SessionFactory fabricaDeSessao = buildFabricaDeSessao();
    

    Obs, I did not understand why the project did not create the web-in folder. I will create in the hand and put the web.xml there, since it is also giving error for lack of this file.

      

    The webapp folder is empty. It has no web-inf or meta-inf. The full path is:

    C: \ sampleSpring \ Drugstore \ src \ main \ webapp >

    Follow the structure of web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
    
        <!-- Nome da Aplicação -->
        <display-name>Drogaria</display-name>
    </web-app>
    

    I've got the video description link.

    Anything put the structure of hibernat.cfg.xml and pom.xml

        
    asked by anonymous 09.10.2015 / 13:40

    1 answer

    7

    Come on.

    1st Your web.xml file is inside the webapp folder. Create a folder named WEB-INF and place your web.xml inside it. Another alternative is to delete your web.xml file and do the following:

    Right-click the project, go to Java EE Tools , and then click: Generate Deploy Descriptor Stub . If you do this you will have the WEB-INF folder with your web.xml inside file.

    2nd Your class name is spelled wrong. It should be HibernateUtil , it's HibernatUtil , or it's missing a and . Click on your Class to enter F2 to rename it to HibernateUtil

    3rd The content of your Class is also incorrect, I believe you have forgotten something. Change the contents of your HibernateUtil Class:

    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;
        }
    }
    

    4th In your test class, the one that opens and closes the connection does the following:

    public class HibernateUtilTeste {
    
        public static void main(String[] args) {
            HibernateUtil.getSessionFactory().openSession();
    
            HibernateUtil.getSessionFactory().close();
        }
    }
    

    I'm not going to go into detail about the content of the HibernateUtil class because I think the video you watched has a good explanation.

    After performing all these steps, right click on your project, go to Maven > Update Project > OK

    PS : A tip, always match your code. To do this, just use the combination crtl+shift+f

        
    14.10.2015 / 16:33