-->
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: thoughts?
PostPosted: Wed Dec 09, 2009 11:27 am 
Newbie

Joined: Mon Aug 24, 2009 1:09 pm
Posts: 1
Is there anything inherently wrong with the below code? I'm giving the consumer of the method the ability to pass "%" within the argument so they can use a like statement for the query. Example findContact(company, "Joe%") or findContact(company, "Joe Smith")

Code:
    public List<Contact> findContact(Company company, String name) {
        return query("FROM Contact c WHERE c.company = :company AND c.name " + likeOrEqual(name) + " :name").
                setParameter("company", company).
                setParameter("name", name).
                list();
    }

    private String likeOrEqual(String ref) {
        return ((ref.startsWith("%") || ref.endsWith("%")) ? "LIKE" : "=");
    }



Top
 Profile  
 
 Post subject: Re: thoughts?
PostPosted: Wed Dec 09, 2009 11:46 am 
Newbie

Joined: Thu Aug 13, 2009 9:58 am
Posts: 5
How about using the Criteria API?

Code:
public List<Contact> findContact(Company company, String name) {
  return myDAO.createCriteria(Contact.class)
    .add(Resctictions.eq("c.company", company)
    .add(Restrictions.ilike("c.name", name, MatchMode.ANYWHERE)
    .list();
}


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.