Hibernate version used with Netbeans 6.1
Database: Mysql 5
Hi guys, i need a little help with this. I have a relations one to many betwwen 2 tables.
Well, this is the code neccesary to put to work this.
public class Usuario implements java.io.Serializable {
private int _id = 0;
private String _codigo;
private String _clave;
private String _preguntasecreta;
private String _respuestasecreta;
private PreguntaSecreta _pregunta;
...properties
}
public class PreguntaSecreta implements java.io.Serializable {
private int _id = -1;
private String _pregunta;
...properties
}
<hibernate-mapping>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="entidades.seguridad.Usuario" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="usuario">
<id column="id" name="_id" unsaved-value="0">
<generator class="increment"/>
</id>
<property column="clave" name="_clave"/>
<property column="codigo" name="_codigo"/>
<property column="preguntasecreta" name="_preguntasecreta"/>
<property column="respuestasecreta" name="_respuestasecreta"/>
<many-to-one column="preguntasecreta_id" name="_pregunta"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="entidades.seguridad.PreguntaSecreta" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="preguntasecreta">
<id column="id" name="_id" unsaved-value="0">
<generator class="increment"/>
</id>
<property column="pregunta" name="_pregunta"/>
</class>
</hibernate-mapping>
Well, the problem is when i want to query the database
public List<Usuario> Busca(String filtro)
{
List<Usuario> items = null;
Transaction trans = null;
try {
trans = _session.beginTransaction();
Query q = _session.createQuery("from entidades.seguridad.Usuario u where u._codigo like ?");
q.setString(0, "%"+filtro+"%");
items = q.list();
trans.commit();
}
catch(Exception e) {
System.out.println(e.getMessage());
trans.rollback();
}
finally {
_session.close();
}
return items;
}
In this method, im asking the database for all the users that contains in the field "codigo" the param filtro. In the client side, when i want to do this
System.out.println( "Inicia:" );
List<Usuario> items = Autenticar("Mig");
System.out.println( items.get(0).get_codigo() );
System.out.println( items.get(0).get_pregunta().get_id() );
The query has results, it prompts...
Inicia:
Miguel
null
I think the problem with this is that hibernate is not retrieving the data for the PreguntaSecreta class in get_pregunta(). I dont know, maybe i have to put a special word in the mapping file to do this or in the Query class.
Extra info: Hibernate is ok when i query for info of a single table.
hEakfall.