java 如何使用 EclipseLink 在仅具有唯一值的列中查找值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11034322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:32:17  来源:igfitidea点击:

How do I find a value in a column that just have unique values with EclipseLink?

javasqljpaeclipselink

提问by Rox

You have the EntityManager.find(Class entityClass, Object primaryKey)method to find a specific row with a primary key.

您可以EntityManager.find(Class entityClass, Object primaryKey)使用主键查找特定行。

But how do I find a value in a column that just have unique values and is not a primary key?

但是如何在只有唯一值而不是主键的列中找到一个值?

采纳答案by maksimov

For example, like this:

例如,像这样:

List<T> results = em.createQuery("SELECT t FROM TABLE t", T.class)
                        .getResultList();

With parameters:

带参数:

List<T> results = em.createQuery("SELECT t FROM TABLE t where t.value = :value1")
                        .setParameter("value1", "some value").getResultList();

For single result replace getResultList()with getSingleResult():

对于单个结果替换getResultList()getSingleResult()

T entity = em.createQuery("SELECT t FROM TABLE t where t.uniqueKey = :value1")
                 .setParameter("value1", "KEY1").getSingleResult();

One other way is to use Criteria API.

另一种方法是使用 Criteria API。

回答by Pau Kiat Wee

You can use appropriate JPQL with TypedQuery.

您可以将适当的 JPQL 与TypedQuery一起使用

try {
    TypedQuery<Bean> tq = em.createQuery("from Bean WHERE column=?", Bean.class);
    Bean result = tq.setParameter(1, "uniqueKey").getSingleResult();
} catch(NoResultException noresult) {
    // if there is no result
} catch(NonUniqueResultException notUnique) {
    // if more than one result
}

回答by James

You can use a Query, either JPQL, Criteria, or SQL.

您可以使用查询,JPQL、Criteria 或 SQL。

Not sure if your concern is in obtaining cache hits similar to find(). In EclipseLink 2.4 cache indexes were added to allow you to index non-primary key fields and obtain cache hits from JPQL or Criteria.

不确定您是否关心获取类似于 find() 的缓存命中。在 EclipseLink 2.4 中添加了缓存索引以允许您索引非主键字段并从 JPQL 或 Criteria 获取缓存命中。

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Indexes

见, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Indexes

Prior to 2.4 you could use in-memory queries to query the cache on non-id fields.

在 2.4 之前,您可以使用内存查询来查询非 id 字段的缓存。