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
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=?.
BANK ENTRY
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.
– Richard Willian
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
– LSA
Thanks for the tip @LSA I edited the question.
– Richard Willian
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– Macario1983
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.
– Richard Willian
All right, but the first mistake I saw was this.
– Macario1983
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.
– Richard Willian
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 fileproperties
to enable thesql
ofhibernate
and carry out adebug
more qualified!– Macario1983
That one
properties
would be theshow_sql
? I always use it, very good, makes it really easy to find out what’s going on– Richard Willian
Wow that "back" was heartbreaking, thank you for the correction.
– Richard Willian