-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Not retreiving data for class in foreign key. Cascade?
PostPosted: Tue Sep 02, 2008 10:44 am 
Newbie

Joined: Tue Sep 02, 2008 10:01 am
Posts: 3
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.

Image

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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 2:01 pm 
Newbie

Joined: Sat Aug 02, 2008 12:09 pm
Posts: 12
I have not worked with the raw transaction object for a while, but I guess it could be the fact that you commit before you return the list, and the entity becomes detached.

Someone correct me if i'm wrong :-)

Either change the way you handle transactions or configure eager loading (or just try to iterate the list before you commit, as a quick test)

cheers

G


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 10:10 am 
Newbie

Joined: Tue Sep 02, 2008 10:01 am
Posts: 3
hi, i really dont know how to do this, i guess im not the first person with this problem. Maybe there is something that i miss on the docs.

Goran ty for ur post, i iterated the query results without calling the method get_pregunta().get_id() and all is ok, there is no exception. But if i call this method, a beatifull expection is showed:D


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 8:17 am 
Newbie

Joined: Tue Sep 02, 2008 10:01 am
Posts: 3
hmm guys, i need help with this, i still cant make it works T_T. Maybe, there is another way to do this, but i dont know it T_T .


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.