简体   繁体   中英

issue with like in hibernate hql

I'm developping a method that takes as a parameter the criterion research in the Query and the text of the beginning of the value of the criterion but each times I test it so I'm getting an empty list , I need some Help please

public ArrayList<Article> getArticleByCritere(String critere, String txt){
               ArrayList list = new ArrayList<Article>();
               list=null;
               String cr;
        try {
            this.session = HibernateUtil.getSessionFactory().openSession();
            org.hibernate.Transaction tx = session.beginTransaction();

        if(critere.equals("Référence"))
            cr="refa";
        else if(critere.equals("Désignation"))
            cr="designation";
        else if(critere.equals("Famille"))
            cr="famille";
        else if(critere.equals("Code"))
            cr="codeArticle";
        else
            cr = "sousFamille";
        String query = "from Article where :critere like :debut";
       list = (ArrayList<Article>) session.createQuery(query).setString("critere", cr).setString("debut", txt + "%").list();
              tx.commit();
            System.out.println("ok");
            session.close();
        } catch (Exception e) {
            System.out.println(" getArticleByFamDesign a échoué" + e);
        }
        return list;
          }
could any one help me to find what's the problem here !!

You can always pass values as query parameters. You can't pass rando parts of the query, like column names.

So the code should be:

String query = "from Article where " + cr + " like :debut";
list = (List<Article>) session.createQuery(query)
                              .setString("debut", txt + "%")
                              .list();

Also, not the following points.

Query.list() returns a List, and the documentation doesn't guarantee that the list is an ArrayList. You shouldn't cast the result to ArrayList. And in fact, you have no reason at all to do that. Why would you care about the concrete implementation of the list. All that matters is that it's a List. Your methd should also return a List<Article> , and not an ArrayList<Article> .

These two lines of code make absolutely no sense:

ArrayList list = new ArrayList<Article>();
list = null;

First of all, you don't need to declare the list at this point. Only declare it when you need it. Second, you shouldn't use raw types. Always specify the generic type of the collection. Third, What's the point in creating a new ArrayList object, only to throw it to the garbage bin right after by reinitializing list to null?

Finally, your exception handling is awful. Only swallow exceptions when you can do something meaningful to solve the problem. Returning null rather than the actual result is much worse than letting the exception propagate. Now you don't know why you get null, and since you haven't even printed the exception, you can't even diagnose what the problem might be.

The way to handle exception, transactions and sessions is described in the hibernate documentation . The session should be closed in a finally block, to be absolutely sure that it's closed. Not closing it will leave a database connection open forever. Do that 50 times, and your application wont't be able to connect to the database anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM