【问题标题】:objectdb select where queryobjectdb select where 查询
【发布时间】:2013-06-16 18:01:05
【问题描述】:

我尝试使用 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 的列表。我的选择查询有什么问题?

【问题讨论】:

  • 你作为 x1 和 x2 传入了什么?
  • 我调用了一个函数List&lt;RSSItem&gt; berita = RSSItem.getRSSItems(1, 500);,所以当它通过时,x1 应该是 1,x2 是 500。

标签: where objectdb


【解决方案1】:

使用值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。

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多