Error analysis and correction in DB connection test using Hibernate

Asked

Viewed 321 times

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’re making the mistakes:

  1. Configuration cannot be resolved Hibernatutil.java /Drogaria/src/main/java/br/drogaria/util line 17 Java Problem Description Resource Path Location Type

The line is:

ServiceRegistry registro = new StandardServiceRegistryBuilder()
                              .applySettings(configuration.getProperties()).build();
  1. Configuration cannot be resolved Hibernatutil.java /Drogaria/src/main/java/br/drogaria/util line 18 Java Problem Description Resource Path Location Type

The line is:

SessionFactory fabricaDeSessao = configuration.buildSessionFactory(registro);
  1. Hibernateutil cannot be resolved Hibernatutilteste.java /Drogaria/src/main/java/br/drogaria/main line 7 Java Problem Description Resource Path Location Type

The line is:

HibernateUtil.getFabricaDeSessao().openSession();
  1. The method buildFabricaDessao() is Undefined for the type Hibernatutil Hibernatutil.java /Drogaria/src/main/java/br/drogaria/util line 9 Java Problem Description Resource Path Location Type

The line is:

private static final SessionFactory fabricaDeSessao = buildFabricaDeSessao();

Obs, I don’t understand why the project didn’t create the web-in folder. I will create by hand and put the web.xml there, since it is also giving error by missing this file.

The webapp folder is empty. It has neither web-inf nor meta-inf. The complete path is:

C: For exampleSpring Drogaria src main webapp >

Follows 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 got the video description link.

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

  • 1

    @Techies If you can help me, I’m grateful, beauty? I changed some things, mainly in the name of objects

  • 1

    Read the last error, it says it did not find the file web.xml. You didn’t accidentally cut him out?

  • As @jbueno said, the file is missing web.xml

  • @jbueno the web.xml file is here at the root of the project. Actually, I saw this error, but it should not be missing in this detail.

  • At the root of the project Voce says would be in the folder WEB-INF ?

  • @jbueno I’ve already edited.

  • @Techies No. It’s in the Drugstore folder, which is the name of the artifact. It should be in web-inf?

  • Yeah, you should be in Webapp > WEB-INF

Show 4 more comments

1 answer

7


Come on.

first Your file web.xml is inside the folder webapp. Create a folder called WEB-INF and put your web.xml inside it. Another alternative is to delete your file web.xml and do the following:

Right click on the project, go to Java EE Tools and then click on: Generate Deploy Descriptor Stub. Done this You will have the folder WEB-INF with your file web.xml within.

2nd Your class name is spelled wrong. It should be HibernateUtil, is HibernatUtil, or if one is missing and. Click on your Class F2 rename her to Hibernateutil

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

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

fourth In your test class, the one that opens and closes the connection do the following:

public class HibernateUtilTeste {

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

        HibernateUtil.getSessionFactory().close();
    }
}

I will not go into detail about the content of the Class HibernateUtil because I believe in the video you watched there’s a good explanation.

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

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

  • I’ll make the changes, I’ll tell you anything.

  • All right @Andrénascimento

Browser other questions tagged

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