0

so i am working on a project right now 1st time using Hibernate in this projet i am using Swing too i have a form with multiple jTextFields

 public List<Object[]> getoperations(String a,String c,String n,String e,String d) {
     SessionDao s=new SessionDao();
     session=s.getSession();
     Query q;
     q=session.createQuery("select idTiers,beneficiaire,emetteur,montant,numcompte,t_param_nature_operation.libelleNature,dateValidite,dateCreation where");
     if (a != null && !a.isEmpty()) { q+= " and codeBanque='" + a + "'"; }
     if (c != null && !c.isEmpty()) { q += " and numCompte='" + c + "'"; }
     if (n != null && !n.isEmpty()) { q += " and t_param_nature_operation_.libelleNature='" + n + "'"; }
     if (e != null && !e.isEmpty()) { q += " and decision='" + e + "'"; }
     if (d != null && !d.isEmpty()) { q += " and dateCreation='" + d + "'"; }

    q+= " order by idTiers" ;
     return q.list();

 }

As you see I am making a test on the values to add them in the query. My question is there a way to add those values? since query +="" isn't working.

xenteros
  • 15,586
  • 12
  • 56
  • 91
Unknwon
  • 7
  • 4
  • Personally, I would add Guava utils to my project and use `isNotBlank()` function. Anyway, you can write your own `static` function that would return true `if not null and not empty` and false otherwise, and later use it. It'll make your code much clearer. – xenteros Jul 25 '16 at 08:33
  • Let me advice you formatting your code logically. Read some good practices and decide if you want to use spaces (better solution) between operators and variables or not. It's difficult to read if two identical `if`s are formatted in a different way. – xenteros Jul 25 '16 at 08:44
  • If I have helped you at any point feel free to mark my answer as the one that has helped you. This is how our community works. – xenteros Jul 25 '16 at 11:35

2 Answers2

0

you should consider using Criteria. it's more clean when dealing with multiple where statements.

eg

Criteria cr = session.createCriteria(YourEntityClass.class);
cr.add(Restrictions.eq("property1", value1));
cr.add(Restrictions.eq("property2", value2));
List results = cr.list();

have a look at these examples here

Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • thank you for the response.i have one last question about criteria.it will only select the columns in the restrictions ? like if i create criteria and add a restriction on salary.it will show only the results from the column salary? – Unknwon Jul 25 '16 at 18:54
  • No, it will select the entire entity, all the columns – Apostolos Jul 25 '16 at 21:41
0

Personally, I would add Guava utils to my project and use isNotBlank() function. Anyway, you can write your own static function that would return true if not null and not empty and false otherwise, and later use it. It'll make your code much clearer.

The above was my comment and I decided to show you this little piece of code.

public static boolean isBlank(String s) {
    if (s == null)
        return true;
    if (s.isEmpty())
        return true;
    return false;
}

Now you can simply write:

//static import your isBlank() method
//import static package.classInWhichIsBlankIsDeclared;

 if (!isBlank(a) { q+= " and codeBanque='" + a + "'"; }
 if (!isBlank(b) { q+= " and codeBanque='" + b + "'"; }
 if (!isBlank(c) { q+= " and codeBanque='" + c + "'"; }
 if (!isBlank(d) { q+= " and codeBanque='" + d + "'"; }

It's much more readable so it'll be much easier to debug in case of errors in the future.

Please, have a look at DRY principle and follow it. If your issue require checking same condition 4 or 5 times (2 times should be enough to use DRY) consider writing a function. call it the way that it'll be human-friendly instead of combination of different logical statements.

DRY. Don't Repeat Yourself.

"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system"

Wikipedia article about DRY

xenteros
  • 15,586
  • 12
  • 56
  • 91