-->
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.  [ 5 posts ] 
Author Message
 Post subject: HQL to Criteria
PostPosted: Thu Apr 26, 2007 10:25 am 
Newbie

Joined: Thu Apr 26, 2007 10:21 am
Posts: 3
Hi!

I've this mapping:
-----------------------------
<hibernate-mapping package="mm.sistema.model">

<class name="Terceiro" table="terceiros">
<id name="codigo" column="codigo">
<generator class="sequence">
<param name="sequence">ter_seq</param>
</generator>
</id>

<property name="nome" />
<property name="email" />
<property name="fantasia" column="nome_fantasia" />
</class>

<class name="Terceiro$Tipo" table="terceiros_tipos">
<composite-id>
<key-property name="empresa" column="emp_codigo" />
<key-property name="terCodigo" column="ter_codigo" />
<key-property name="tipo" />
</composite-id>

<many-to-one name="terceiro" class="Terceiro" insert="false" update="false" fetch="join" cascade="none">
<column name="ter_codigo" />
</many-to-one>

<property name="dataCadastro" column="data_cadastro" />
<property name="ativo" />
</class>
</hibernate-mapping>
-----------------------------

This HQL returns what I want:
getSession().createQuery(
"select terceiro from Terceiro$Tipo tertip where tertip.terceiro.codigo = 4 and tertip.tipo = 8")
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .list()

How do I write this using Criteria?
Note: I want to return List of Terceiro, not list of Terceiro.Tipo or of array.

Thanks,


Adriano


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 1:41 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
The where clause seems straightforward. Did you try Projections?

You do something like:
Code:
session
         .createCriteria(Tercero.Tipo.class)
         .add(Restrictions(...))
         .setProjection(Projections.projectionList("tercero"));

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 6:45 am 
Newbie

Joined: Thu Apr 26, 2007 10:21 am
Posts: 3
batmat wrote:
The where clause seems straightforward. Did you try Projections?

You do something like:
Code:
session
         .createCriteria(Tercero.Tipo.class)
         .add(Restrictions(...))
         .setProjection(Projections.projectionList("tercero"));


It works, thanks! :-)
But there is still a problem.

It's doing another SELECT to retrieve each item.
The HQL query uses join, I want criteria with joins too.

Can I do this?

Thanks,


Adriano


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 7:46 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Add the right setFetchMode("yourRelation",FetchMode.JOIN) on your criteria.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 8:07 am 
Newbie

Joined: Thu Apr 26, 2007 10:21 am
Posts: 3
batmat wrote:
Add the right setFetchMode("yourRelation",FetchMode.JOIN) on your criteria.


I tried:
Code:
Criteria criteria = getSession().createCriteria(Terceiro.Tipo.class)
    .setProjection(Projections.projectionList().add(
        Projections.property("terceiro")))
    .setMaxResults(Properties.maxRecords)
    .setFetchMode("Terceiro", FetchMode.JOIN)
;


and:
Code:
Criteria criteria = getSession().createCriteria(Terceiro.Tipo.class)
    .setProjection(Projections.projectionList().add(
        Projections.property("terceiro")))
    .setMaxResults(Properties.maxRecords)
    .createCriteria("terceiro", Criteria.INNER_JOIN)
    .setFetchMode("terceiro", FetchMode.JOIN)
;


And it do:

select * from ( select this_.ter_codigo as y0_ from terceiros_tipos this_ inner join terceiros terceiro1_ on this_.ter_codigo=terceiro1_.codigo ) where rownum <= ?

select terceiro0_.codigo as codigo6_0_, terceiro0_.nome as nome6_0_, terceiro0_.email as email6_0_, terceiro0_.nome_fantasia as nome4_6_0_ from terceiros terceiro0_ where terceiro0_.codigo=?

I'm using Oracle.

Any other tip?

Thanks,


Adriano


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.