Query using Hibernate does not bring record

Asked

Viewed 135 times

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

The query is made because it shows the query mounted by Hibernate. But it does not bring any record, even if it exists in the database.

Mounted query compares the log ID I need with a question mark (?).

This is the assembled 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:

FILE LOCATION

inserir a descrição da imagem aqui

HIBERNATE CONFIGURATION

<?xml version="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>

PLACE OF TROUBLE

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

CONSULTATION MOUNTED BY HIBERNATE

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=?.

inserir a descrição da imagem aqui

BANK ENTRY

inserir a descrição da imagem aqui

I am new to programming and do not know exactly how to use Hibernate and if it is correct what I am doing, if you can please give me some tip how to improve I thank.

  • If you need any other information, please let me know so I can edit the question.

  • Instead of photos, place the code directly. You can read a little more at: https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485

  • Thanks for the tip @LSA I edited the question.

  • 1

    Buddy, if I didn’t get it wrong, you’re doing it wrong in your code, you set the code wrong, note here: Usuario usuario = (Usuario) session.get(Usuario.class, new Integer(1));. And in the bank query is the id as 2

  • 1

    Our @Macario1983 truth even, thank you for the remark, but still did not bring the record, I discovered why was giving the error, I am making the answer here, I will send already.

  • All right, but the first mistake I saw was this.

  • 1

    Thanks for seeing that, I already corrected there so that there is no doubt about it. The problem I was having was something so not intuitive for beginners like me. I’ll put the answer.

  • All one day we were beginner in something, and we will always be in others, for that, always use the debug, a hint, has how you create a file properties to enable the sql of hibernate and carry out a debug more qualified!

  • That one properties would be the show_sql ? I always use it, very good, makes it really easy to find out what’s going on

  • Wow that "back" was heartbreaking, thank you for the correction.

Show 5 more comments

1 answer

2


The problem was solved as follows:

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

But it was creating a banco de dados called EcoChatin raiz of my project. See in the image:

inserir a descrição da imagem aqui

Thus, the database that was being accessed was the one at the root, but what I was inserting records using the hsqldb was another bank, located in another folder, see in the image:

inserir a descrição da imagem aqui

That way, I would consult a bank, but actually I would like to be accessing another.

To solve the problem, I changed the configuration shown above: <property name="connection.url">jdbc:hsqldb:EcoChat</property>

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

So I can retrieve the information located in the innermost bank.

Browser other questions tagged

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