Query using Hibernate does not record

3

I'm trying to query the HSQLDB database using Hibernate but I'm not succeeding.

When I do:

session.get(Usuario.class, new Integer(1));

Query is done because it shows the query mounted by Hibernate. But it does not have any registry, even if it exists in the bank.

The mounted query compares the record ID I need with a question mark (% with%).

This is the mounted query:

select usuario0_.ID as ID1_0_0_, usuario0_.NOME as NOME2_0_0_,
       usuario0_.EMAIL as EMAIL3_0_0_, usuario0_.SENHA as SENHA4_0_0_
from USUARIOS usuario0_ where usuario0_.ID=?

Below are some images that may be needed:

LOCATION OF FILES

HIBERNATECONFIGURATION

<?xmlversion="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:EcoChat</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</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.NoCacheProvider</property>

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

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- Mapping files -->
        <mapping resource="ecochat/dao/modelos/usuario/mapeamento/usuario.hbm.xml" />
    </session-factory>
</hibernate-configuration>

USER MAPPING

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="ecochat.entidades.modelos.Usuario" table="USUARIOS">
        <id name="id" type="int" column="ID">
            <generator class="native" />
        </id>

        <property name="nome">
            <column name="NOME" />
        </property>
        <property name="email">
            <column name="EMAIL" />
        </property>
        <property name="senha">
            <column name="SENHA" />
        </property>
    </class>
</hibernate-mapping>

PROBLEM LOCATION

public static void main(String[] args) {
        try {
            //UIJanelaServidorCentralChat.getInstance();
            Configuration config = new Configuration();
            config.configure("ecochat\hibernate\configuracoes\hibernate.cfg.xml");
            Logger.getLogger("org.hibernate").setLevel(Level.OFF);

            SessionFactory factory = config.buildSessionFactory();

            Session session = factory.openSession();
            session.getTransaction().begin();
            Usuario usuario = (Usuario) session.get(Usuario.class, new Integer(2));
            System.out.println(usuario.getNome());
        } catch(HibernateException exception){
             exception.printStackTrace();
        } catch (Exception e) {
            System.err.println("Ops! " + e.getMessage() + "\n");
        }
    }

HIBERNATE ASSEMBLY

select usuario0_.ID as ID1_0_0_, usuario0_.NOME as NOME2_0_0_, usuario0_.EMAIL as EMAIL3_0_0_, usuario0_.SENHA as SENHA4_0_0_ from USUARIOS usuario0_ where usuario0_.ID=?.

NOBANKREGISTRATION

I'm new to programming and I do not know exactly how to use Hibernate and if it's correct what I'm doing, if you can please give me some tips on how to improve, thank you.

    
asked by anonymous 19.05.2018 / 17:09

1 answer

2

The problem was resolved as follows:

When I set up the hibernate.config.xml I used the url line: <property name="connection.url">jdbc:hsqldb:EcoChat</property> this setting.

But this was creating a banco de dados called EcoChat in raiz of my project. See the picture:

Inthisway,thebankthatwasbeingaccessedwastherootone,butwhatIwasinsertingrecordsusingthehsqldbinterfacewasanotherbank,locatedinanotherfolder,seetheimage:

Thatway,Iwouldrefertoabank,butinfactI'dliketobeaccessinganother.

Tosolvetheproblem,Ichangedtheconfigurationshownabove:<propertyname="connection.url">jdbc:hsqldb:EcoChat</property>

to: <property name="connection.url">jdbc:hsqldb:file:Bibliotecas/hsqldb/data/EcoChat</property>

So I can recover the information located on the innermost bank.

    
19.05.2018 / 19:37