-->
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.  [ 2 posts ] 
Author Message
 Post subject: problems with Criteria
PostPosted: Fri Oct 03, 2014 8:14 am 
Newbie

Joined: Fri Oct 03, 2014 7:46 am
Posts: 3
Hello

I have 2 tables documents and document_sections
in documents I have relation ManyToOne to document_sections
I have entities for these tables

I need criteria that will work like next SQL
Code:
SELECT t1.*
FROM documents AS t1
LEFT JOIN document_sections AS t2
WHERE t1.fileName = some_vaslue AND
t2.applicationSection = some_vaslue1

So I want to use both tables in where clause but have only documents in result
I've tried different things to do and below I put one variant
Code:
DetachedCriteria dc = DetachedCriteria.forClass(Document.class);
if(StringUtils.isNotBlank(searchDocumentName)){
   dc.add(Restrictions.like("fileName", searchDocumentName));
}
if(selectedApplicationSection != null){
   dc.createAlias("documentSection", "ds", JoinType.INNER_JOIN,
         Restrictions.eq("ds.applicationSection", selectedApplicationSection));

when I try to get result list - I get List<Object[]> instead List<Documents>
and this array of objects contains both Entities: DocumentSection and Document
so result like was exacuted next query:
Code:
SELECT t1.*, t2.*
FROM documents AS t1
LEFT JOIN document_sections AS t2
WHERE t1.fileName = some_vaslue AND
t2.applicationSection = some_vaslue1


Can somebody help me?


Top
 Profile  
 
 Post subject: found solution
PostPosted: Wed Dec 24, 2014 12:23 pm 
Newbie

Joined: Fri Oct 03, 2014 7:46 am
Posts: 3
The problem was little bit another then I've described:
I've not told that I've run 2 queries using this DetachedCriteria
1st with Projection count
and after this I set projections to null and get result list using criteria
and in this case before get list - need to write next code:
Code:
criteria.setResultTransformer(Criteria.ROOT_ENTITY);


if try to get result without
Code:
criteria.setProjection(null);
before - then all works perfectly


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