-->
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.  [ 3 posts ] 
Author Message
 Post subject: Does hibernate have an equivalent option to this ibatis feat
PostPosted: Fri Apr 16, 2004 10:20 am 
Beginner
Beginner

Joined: Mon Mar 22, 2004 9:37 am
Posts: 22
Location: Willow Grove, PA
ure. ?

Code:
<statement id="dynamicGetAccountList"
resultMap="account-result" >
select * from ACCOUNT
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="firstName">
(ACC_FIRST_NAME = #firstName#
<isNotNull prepend="OR" property="lastName">
ACC_LAST_NAME = #lastName#
</isNotNull>
)
</isNotNull>
<isNotNull prepend="AND" property="emailAddress">
ACC_EMAIL like #emailAddress#
</isNotNull>
<isGreaterThan prepend="AND" property="id" compareValue="0">
ACC_ID = #id#
</isGreaterThan>
</dynamic>
order by ACC_LAST_NAME
</statement>


i.e. Is there a way to pre-define a dynamic query like this? Is something like this planned?
Should I just go foward with a brute force string concatination for a query?

Thanks,

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 10:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use a Criteria query.


Top
 Profile  
 
 Post subject: Thanks, pretty simple answer
PostPosted: Mon Apr 19, 2004 4:11 pm 
Beginner
Beginner

Joined: Mon Mar 22, 2004 9:37 am
Posts: 22
Location: Willow Grove, PA
The criteria api mostly does what I am looking for.

However, I believe there is a bug.

Give the function below

crit.add(Expression.eq("model.description", map.get("modelDesc")));

should be the same as

modelCriteria = crit.createCriteria("model");

modelCriteria.add(Expression.eq("description", map.get("modelDesc")));

However, the first method throws a QueryException ("could not resolve property") . According to page 218 of the Hibernate in action(sample chapter), I can dereference child objects with the . notation.


Code:
public List queryByCriteriaMap(Map map) throws ServiceException, HibernateException {
   
    List list = null;
    Criteria modelCriteria = null;
    Criteria crit = this.getSession().createCriteria(Netelement.class);
   
    if (map.containsKey("elementName")) {
        crit.add(Expression.eq("name", map.get("elementName")));
    }
   
    if (map.containsKey("vendorName")) {
        if (modelCriteria ==null) {
            modelCriteria = crit.createCriteria("model");
        }
       
        modelCriteria.createCriteria("vendor").add(Expression.eq("name", map.get("vendorName")));
    }
   
    if (map.containsKey("modelDesc")) {
        if (modelCriteria ==null) {
            modelCriteria = crit.createCriteria("model");
        }
        //crit.add(Expression.eq("model.description", map.get("modelDesc")));
        modelCriteria.add(Expression.eq("description", map.get("modelDesc")));
    }
   
    if (map.containsKey("type")) {
        if (modelCriteria ==null) {
            modelCriteria = crit.createCriteria("model");
        }
       
        modelCriteria.createCriteria("deviceType").add(Expression.eq("name", map.get("type")));
    }
   
   
    if (map.containsKey("ipaddr")) {
        crit.add(Expression.eq("ipaddr", map.get("ipaddr")));
    }
   
    if (map.containsKey("globalsync")) {
       
   
        crit.add(Expression.eq("globalsync", map.get("globalsync")));
    }
   
    if (map.containsKey("agentName")) {
        Criteria agentCriteria = crit.createCriteria("Agents");
        agentCriteria.add(Expression.eq("name", map.get("agentName")));
    } 
   
    if (map.containsKey("containerName")) {
        Criteria containerCriteria = crit.createCriteria("containers");
        containerCriteria.add(Expression.eq("name", map.get("containerName")));
    }
    return crit.list();
   
}


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