0

I attempt to select some instance using where clause

public static List<RSSItem> getRSSItem(int x1, int x2) {
     EntityManagerFactory emf = DBHandler.getEmf();
    EntityManager em = DBHandler.getEm(emf);
    String query =
            "SELECT items FROM RSSItem items "
            + "WHERE items.id <= :x1 AND "
            + "items.id >= :x2";
    List<RSSItem> results =
            (List<RSSItem>) em.createQuery(query).
            setParameter("x1", x1).
            setParameter("x2", x2).
            getResultList();
    return results;
}

the RSSItem attributes :

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
String title;
String link;
String description;
String pubdate;
String content;
HashMap<String, Integer> keyword = new HashMap();
HashMap<String, Integer> keywordBefore = new HashMap();
// TreeMap <String, Integer> keyword = new TreeMap();
String feed;

the problem is it always return a list with 0 size. what's wrong with my select query?

4

1 回答 1

2

使用值x1 = 1x2 = 500,查询变成...

SELECT items FROM RSSItem items
 WHERE items.id <= 1 
   AND items.id >= 500

由于同时没有 id less or equal to 1greater or equal to 500查询将不会给出任何命中。你想要的可能是;

String query =
        "SELECT items FROM RSSItem items "
        + "WHERE items.id >= :x1 AND "
        + "items.id <= :x2";

...使用您的示例数据将找到 1 到 500 之间的所有 id,包括 1 到 500。

于 2013-06-17T06:11:48.233 回答