我尝试使用where子句
选择一些实例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;
}
RSSItem属性:
@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;
问题是它总是返回一个0大小的列表。我的选择查询出了什么问题?
答案 0 :(得分:2)
使用值x1 = 1
和x2 = 500
,查询将变为...
SELECT items FROM RSSItem items
WHERE items.id <= 1
AND items.id >= 500
由于同时没有id less or equal to 1
和greater or equal to 500
,因此查询不会给出任何匹配。你想要的可能是什么;
String query =
"SELECT items FROM RSSItem items "
+ "WHERE items.id >= :x1 AND "
+ "items.id <= :x2";
...在您的示例数据中,您将找到1到500之间的所有ID,包括在内。