-->
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: Criteria, subselect
PostPosted: Tue Sep 30, 2008 2:41 pm 
Newbie

Joined: Sat Aug 02, 2008 6:41 am
Posts: 3
Hello. I really hope, that you can help me.

I have entity class, called property and it has OneToMany relation with Image:



Code:
private Set<Image> images;

    @OneToMany(fetch = FetchType.EAGER)
    @Where(clause="source_type = 1")
    @JoinColumn(name="source_id", nullable = true)
    public Set<Image> getImages() {
        return images;
    }

    public void setImages(Set<Image> images) {
        this.images = images;
    }


I use Criteria API for search, but I must get exactly ONE image per property. So select querry has to be like this:

Code:
select p.id, p.something1, p.something2, (select i.file from image where i.sourceId = p.id LIMIT 1), p.something3 FROM property p WHERE ................... LIMIT 20


or
Code:
select DISTINCT ON(p.id) p.id. p.something1, p.something2, i.file
from property p inner join image i where ............... LIMIT 20



Currently, I make LEFT JOIN with Image, but of course, it is not, that I need:

Code:
            Criteria imageCrit = propertyCrit.createCriteria("images", "i",     CriteriaSpecification.LEFT_JOIN);

            offerCrit.setFirstResult(offset);
            offerCrit.setMaxResults(limit);
           
            offerCrit.setProjection(projectionList().create().
                    add(property("bt.parentId")).add(property("o.id")).
                    add(property("ot.name")).add(property("bt.name")).
                    add(property("c.currency")).add(property("o.price")).
                    add(property("o.priceSqm")).add(property("p.nrRooms")).
                    add(property("p.nrBedrooms")).add(property("p.name")).
                    add(property("i.file")). //HERE IT IS
                    add(property("p.id")).
                    add(property("l.name")).add(property("o.areaWholeBuilt")).
                    add(property("o.ranking")).add(property("o.stars")).add(property("p.id")));


Is it possible, to make "subselect" with Projections??
I tried to insert this subselect in sqlProjection, but not succeeded with this.
unfortunately, DISTINCT_ROOT_ENTITY can't help me in this situation..
Please help!!

P.S. sorry for my english
P.S. Hibernate version - 3.2.6ga[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 5:36 pm 
Newbie

Joined: Sat Aug 02, 2008 6:41 am
Posts: 3
I've found the solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 3:30 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Maksim.Solovjov wrote:
I've found the solution.

Ok, then why don't you post it here?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 8:56 am 
Newbie

Joined: Fri Dec 14, 2007 8:34 am
Posts: 3
Hi all,

I am having a similar problem...

I want to translate the following SQL into a Criteria or HQL so that I can use in the application.

Code:
SELECT list.* ,(select count(*)
FROM listmember lm where lm.listid=list.id group by lm.listid ) as memberCount
from mylists list
order by memberCount asc


I would really appriciate any pointers.

Thanks, Jeevan.


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.