-->
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: DetachedCriteria and FKs...
PostPosted: Thu Aug 03, 2006 11:08 am 
Newbie

Joined: Tue Nov 08, 2005 8:31 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2 rc2
Mapping documents:

<class name="br.com.vivo.grc2.model.ServicoDocumentacao" table="GR2_SERVICO">
<id name="id" type="java.lang.Long">...</id>
<one-to-one name="status" class="br.com.vivo.grc2.model.Status" foreign-key="idstatus" cascade="none"/>
<one-to-one name="estado" class="br.com.vivo.grc2.model.temp.Estado" foreign-key="idestado" cascade="none" />
<one-to-one name="servico" class="br.com.vivo.grc2.model.temp.PaServico" foreign-key="idservico" cascade="none"/>
<property name="numeroMovel" not-null="true"/>
<property name="esn" not-null="true"/>
...
</class>

<class name="br.com.vivo.grc2.model.Status" table="GR2_STATUS">
<id name="id" type="java.lang.Long">...</id>
<property name="descricao"/>
<property name="tipo" not-null="true"/>
<one-to-one name="estado" class="br.com.vivo.grc2.model.temp.Estado" foreign-key="idestado"/>
</class>

<class name="br.com.vivo.grc2.model.temp.Estado" table="PA_ESTADO">
<id name="id" type="java.lang.Long">...</id>
<property name="sigla"/>
<property name="descricao"/>
</class>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I am writing a DetachedCriteria object generator method based on
a Class passed and a map of parameter -> vale. Some of these map parameters can be nested (eg.: status.tipo = 'TE')

I would like to create a DetachedCriteria for ServicoDocumentacao class
with: numeroMovel = "5151515151", esn="DBDBDBDB", status.tipo="PE", estado.id = 2.

My generator created:

DetachedCriteria dc = DetachedCriteria.forClass(ServicoDocumentacao.class);

dc.add(Restrictions.eq("numeroMovel", "5184016351"));
dc.add(Restrictions.eq("esn", "DBDBDBDB"));

DetachedCriteria dcTemp;

dcTemp = dc.createCriteria("estado");
dcTemp.add(Restrictions.eq("id", new Long(2)));

dcTemp = dc.createCriteria("status");
dcTemp.add(Restrictions.eq("tipo", "PE"));

A record exists in Oracle Database, but it is not retrieved. It seems like Hibernate is doing some wrong outer joins using two-sides PKs istead FK->PK association.


SQL Generated:

select ...
from GR2_SERVICO this_,
GR2_STATUS status2_,
PA_ESTADO estado5_,
PA_ESTADO estado1_,
PA_SERVICO paservico7_
where this_.id=status2_.id
and status2_.id=estado5_.id(+)
and this_.id=estado1_.id
and this_.id=paservico7_.id(+)

and this_.numeroMovel=?
and this_.esn=?
and estado1_.id=?
and status2_.tipo=?


this_.id=status2_.id ? It should be this_.idstatus=status2_.id
status2_.id=estado5_.id(+) ? It should be status2_.idestado=estado5_.id(+)
this_.id=paservico7_.id(+) ? It should be this_.idservico=paservico7_.id(+)

What is wrong here?

Thank you!


Last edited by lucianog on Thu Aug 03, 2006 1:03 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 11:26 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I think you should have your code as below.

Code:
DetachedCriteria dc = DetachedCriteria.forClass( ServicoDocumentacao.class, "sd" );
dc.add( Restrictions.eq( "sd.numeroMovel", "5184016351") );
dc.add( Restrictions.eq( "sd.esn", "DBDBDBDB") );
dc.createAlias( "sd.estado", "e" );
dc.add( Restrictions.eq( "e.id", new Long( 2 ) ) );
dc.createAlias( "sd.status", "s" );
dc.add( Restrictions.eq( "s.tipo", "PE" ) );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 12:59 pm 
Newbie

Joined: Tue Nov 08, 2005 8:31 pm
Posts: 5
The joins seems to be still wrong. I had already tried that kind of solution using Aliases, but it didn't work. Anyway i tried again:

(I searched status.id istead status.tipo for make it simpler)

select ...
from GR2_SERVICO this_
, GR2_STATUS st2_
, PA_ESTADO estado5_
, PA_ESTADO es1_
, PA_SERVICO paservico7_
where this_.id=st2_.id
and st2_.id=estado5_.id(+)
and this_.id=es1_.id
and this_.id=paservico7_.id(+)

and this_.numeroMovel=?
and this_.esn=?
and es1_.id=?
and st2_.id=?

Is this a bug?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 2:36 pm 
Newbie

Joined: Tue Nov 08, 2005 8:31 pm
Posts: 5
The problem is decided.

I wrote an HQL query for the same need and it still mapped the FKs by wrong, so it drove me to think my mapping was wrong and it really was.

I was mapping some many-to-one relationships as one-to-one.

Thank you


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.