-->
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.  [ 15 posts ] 
Author Message
 Post subject: Can't get "orber by" to work with collection!
PostPosted: Mon Feb 16, 2004 7:05 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.1.1.
The mapping files were generated with Middlegen R3 and can be viewd at (no need to post them again):

http://forum.hibernate.org/viewtopic.ph ... highlight=

---------------------
Testing code
---------------------
Case 1
Code:
( ... )
session = sessionFactory.openSession();
res = session.find("from vo.VersaoDocumento vd where vd.comp_id.documento = ? order by vd.comp_id.id asc", documento_id, Hibernate.LONG);
( ... )


Case 2
Code:
( ... )
session = sessionFactory.openSession();
List res = session.find("from vo.Documento d join fetch d.versaoDocumentos vd where d.id = ? order by vd.comp_id.id asc", documento_id, Hibernate.LONG);
( ... )   


My objective is to use Case 2. I want to retrieve a Documento with the "set" of VersaoDocumento initialized.

My only problem is that I can


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 7:20 am 
Newbie

Joined: Mon Feb 16, 2004 7:11 am
Posts: 13
Mmh, why not use the Criteria class as described in the manual in Chapter 14. If found that a very easy and quick solution. I think it also suits your problem there.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 8:05 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
murxx wrote:
Mmh, why not use the Criteria class as described in the manual in Chapter 14. If found that a very easy and quick solution. I think it also suits your problem there.


Thanks for the reply.
But I tried using the Criteria like this:

Code:
List res = session.createCriteria(Documento.class)
          .createCriteria("versaoDocumentos")                                 
              .add(Expression.eq("comp_id.documento.id", documento_id))
              .addOrder(Order.asc("comp_id.id"))
     .list();


And it gives me the following error:

Code:
java.lang.UnsupportedOperationException: subcriteria cannot be ordered
   at net.sf.hibernate.impl.CriteriaImpl$Subcriteria.addOrder(CriteriaImpl.java:70)
( ... )


Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 8:38 am 
Newbie

Joined: Mon Feb 16, 2004 7:11 am
Posts: 13
My code looks like this and works:

Code:
Criteria c = session.createCriteria(Customer.class);
c.add(Expression.eq("az_kunde_id",new Integer(csf.getCustomerId());
c.add(Expression.like("firma1","%"+csf.getCompany()+"%"));
c.add(Expression.like("name","%"+csf.getName()+"%"));
c.add(Expression.like("vorname","%"+csf.getPrename()+"%"));
c.add(Expression.like("strasse","%"+csf.getStreet()+"%"));
c.add(Expression.like("ort","%"+csf.getTown()+"%"));
c.add(Expression.like("plz","%"+csf.getZip()+"%"));
c.addOrder(Order.asc("az_kunde_id"));
List l = c.list();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 10:03 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
murxx wrote:
My code looks like this and works:

Code:
Criteria c = session.createCriteria(Customer.class);
c.add(Expression.eq("az_kunde_id",new Integer(csf.getCustomerId());
c.add(Expression.like("firma1","%"+csf.getCompany()+"%"));
c.add(Expression.like("name","%"+csf.getName()+"%"));
c.add(Expression.like("vorname","%"+csf.getPrename()+"%"));
c.add(Expression.like("strasse","%"+csf.getStreet()+"%"));
c.add(Expression.like("ort","%"+csf.getTown()+"%"));
c.add(Expression.like("plz","%"+csf.getZip()+"%"));
c.addOrder(Order.asc("az_kunde_id"));
List l = c.list();


I'm trying to implement a join with the Criteria.
That's a simple query.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Is the order by really not included in the generated SQL?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:12 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
Is the order by really not included in the generated SQL?


Thanks for the reply.
Here's my generated sql code:

Code:
Hibernate: select documento0_.id as id0_, versaodocu1_.id as id1_, versaodocu1_.documento_fk as document2_1_, documento0_.nome as nome0_, documento0_.descricao as descricao0_, documento0_.user_id as user_id0_, documento0_.dt_hr as dt_hr0_, documento0_.pasta_documento_fk as pasta_do6_0_, versaodocu1_.descricao as descricao1_, versaodocu1_.user_submissao as user_sub4_1_, versaodocu1_.data_submissao as data_sub5_1_, versaodocu1_.aprovado as aprovado1_, versaodocu1_.user_aprovacao as user_apr7_1_, versaodocu1_.data_aprovacao as data_apr8_1_, versaodocu1_.user_id as user_id1_, versaodocu1_.dt_hr as dt_hr1_, versaodocu1_.id as id__, versaodocu1_.documento_fk as document2___ from documento documento0_ inner join versao_documento versaodocu1_ on documento0_.id=versaodocu1_.documento_fk where (documento0_.id=? ) order by  versaodocu1_.id asc

Hibernate: select documento0_.id as id3_, documento0_.nome as nome3_, documento0_.descricao as descricao3_, documento0_.user_id as user_id3_, documento0_.dt_hr as dt_hr3_, documento0_.pasta_documento_fk as pasta_do6_3_, docproxver1_.documento_fk as document1_0_, docproxver1_.versao_actual as versao_a2_0_, docproxver1_.user_id as user_id0_, docproxver1_.dt_hr as dt_hr0_, pastasdocu2_.id as id1_, pastasdocu2_.nome as nome1_, pastasdocu2_.descricao as descricao1_, pastasdocu2_.user_id as user_id1_, pastasdocu2_.dt_hr as dt_hr1_, pastasdocu2_.pasta_documento_fk as pasta_do6_1_, pastasdocu3_.id as id2_, pastasdocu3_.nome as nome2_, pastasdocu3_.descricao as descricao2_, pastasdocu3_.user_id as user_id2_, pastasdocu3_.dt_hr as dt_hr2_, pastasdocu3_.pasta_documento_fk as pasta_do6_2_ from documento documento0_ left outer join doc_prox_versao docproxver1_ on documento0_.id=docproxver1_.documento_fk left outer join pastas_documento pastasdocu2_ on documento0_.pasta_documento_fk=pastasdocu2_.id left outer join pastas_documento pastasdocu3_ on pastasdocu2_.pasta_documento_fk=pastasdocu3_.id where documento0_.id=?



What's strange is that the order by appears in the generated sql code!
But in my prints to console, I'm getting:

Code:
----- DOCUMENTO -----
ID: 3 + NOME: xpto.pdf
------ VERSAO_DOCUMENTO ------
ID: 2 ++ DESC: bbb
ID: 1 ++ DESC: aaa
------------------------------------


As you can see, my VERSAO_DOCUMENTO aren't appearing orderd ASC.
I've tried putting "order-by="id asc"" in the mapping file for the "set" and that works ok. But I rather want to define my order by's in the code.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:14 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
NOTE: some columns that appear in the sql generated code don't appear in the mapping files I've presented due to simplify reading...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well, you use a Set there I suppose, which are unordered by nature, there is really no way to guarantee a specific iteration order for a HashSet ... you should probably consider using a SortedSet and the sort attribute in the mapping ... never tried this though.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:20 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
Well, you use a Set there I suppose, which are unordered by nature, there is really no way to guarantee a specific iteration order for a HashSet ... you should probably consider using a SortedSet and the sort attribute in the mapping ... never tried this though.


The "order-by" in the mapping works well even with a normal Set. I've tried it and it gave me an ordered Set. And it can be overriden by an "order by" clause in a query for that "set". What's strange is if I don't have that attribute in the mapping, it simply ignores the "order by" clause that I put in the query and gives me an unordered "set" and as you see it appears in the generated code. That's what's strange to me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well if using orderBy, the Set is backed by an underlying LinkedHashSet AFAIK which has a guaranteed iteration order. (Thats why it works only on 1.4. VMs). So why don't you use it if it works for you?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:38 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
Well if using orderBy, the Set is backed by an underlying LinkedHashSet AFAIK which has a guaranteed iteration order. (Thats why it works only on 1.4. VMs). So why don't you use it if it works for you?


First I've never used it like this (in the mapping) because don't know the performance issues on it. If it has performance penalties because of accessing the mapping.
And then I'm curious to know why the problem persists. The code seems clear to me. Need to clarify these problems that I encounter so that I don't have surprises in future projects. :-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 4:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Just confirming that "order by" and "fetch" are not supposed to work together, so this functionality is all correct and expected.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 5:16 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
Just confirming that "order by" and "fetch" are not supposed to work together, so this functionality is all correct and expected.


So the only way I have to do this is at the moment is by putting an "order-by" attribute in the mapping for the lazy loaded "set" , because as in

http://forum.hibernate.org/viewtopic.php?t=928046

Criteria doesn't support "subcriteria ordering".
Any idea on when will we have that kind of functionality?

Thanks for the reply.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 5:23 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
Just confirming that "order by" and "fetch" are not supposed to work together, so this functionality is all correct and expected.


After all I wasn't mistaken when I posted my problem at:

http://forum.hibernate.org/viewtopic.ph ... highlight=

;-)


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