-->
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: Hibernate: Criteria & Subqueries komplex
PostPosted: Thu Feb 24, 2011 8:29 am 
Newbie

Joined: Thu Feb 24, 2011 7:43 am
Posts: 1
Hallo!
ich müsste folgende HQL Statement in Criteria überführen, um die Abfragen dynamisch zu halten:

Code:
SELECT concentration, amount FROM
    (SELECT concentration, count(concentration) as amount FROM
        (SELECT distinct concentration, experiment_fk FROM `cellassay`.`caresult` where experiment_fk =1  or experiment_fk = 3) as a
    GROUP BY concentration) as b
WHERE amount = 2;


Dabei wollte ich experiment_fk =1 or experiment_fk = 3 or..... dynamisch erzeugen, da ein ganzer Array an Restriktionen ausgewählt werden muss.
Mein bisheriger code setzt die innerste Klammer um:

Code:
        Criteria crit = session.createCriteria(CAResult.class);

        String s = filterCriterias.get("id_experiment");
        String[] st= s.split(" ");
        Criterion c = Restrictions.eq("id", new Long(st[0]));
        int amountExpIds=0;
        for(String id : st){
            c = Restrictions.or(c, Restrictions.eq("id", new Long(id)));
            amountExpIds++;
        }   
        crit.createCriteria("experiment").add(c);   
        crit.setProjection(Projections.distinct(Projections.projectionList()               
                    .add(Projections.property("concentration").as("concF"))
                    .add(Projections.property("experiment").as("expeF"))));


Allerdings scheitere an allem weiteren. Sollte ich die innere Klammer in einem DetachedCrtieria darstellen und diese dann als Subquery benutzen?! Wie?
Bin da etwas verzweifelt, aber vielleicht kann mir jemand weiter helfen.
Vielen Dank schon mal!

MP


Top
 Profile  
 
 Post subject: Re: Hibernate: Criteria & Subqueries komplex
PostPosted: Tue Mar 01, 2011 6:31 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Deine Query sieht nicht nach HQL aus, sondern SQL, im From-Teil sind keine Subqueries erlaubt.

Ich denke, die Query kannst du auch so umschreiben, dass sie keine Subselects benötigt:
Code:
SELECT concentration, count(concentration) as amount FROM
    `cellassay`.`caresult` where experiment_fk =1  or experiment_fk = 3 as a
    GROUP BY concentration HAVING amount = 2;

_________________
-----------------
Need advanced help? http://www.viada.eu


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.