0

我正在开发一种方法,该方法将查询中的标准研究和标准值开头的文本作为参数,但每次我测试它,所以我得到一个空列表,我需要一些帮助

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 !!
4

1 回答 1

0

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.

于 2013-07-19T14:04:58.300 回答