java JPA Criteria Api 选择列为空的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11767118/
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
JPA Criteria Api select objects where column is null
提问by Mariusz Grodek
I have a table "word" in PostgreSQL DB:
我在 PostgreSQL 数据库中有一个表“word”:
CREATE TABLE word
(
word_id bigserial NOT NULL,
word character varying(15) NOT NULL,
counter integer NOT NULL,
base_letters character varying(15),
CONSTRAINT word_pk PRIMARY KEY (word_id )
)
And I have a DAO method which has to find all words in table where the column 'base_letters'. I use Spring in general. My method:
我有一个 DAO 方法,它必须在表中找到“base_letters”列所在的所有单词。我一般使用 Spring。我的方法:
public List<Word> getAllWordsWithoutBaseLetters() {
CriteriaQuery<Word> c = cb.createQuery(Word.class);
Root<Word> words = c.from(Word.class);
c.select(words).where(cb.isNull(words.get("base_letters")));
TypedQuery<Word> q = entityManager.createQuery(c);
List<Word> allWordsWithoutBaseLetters = q.getResultList();
return allWordsWithoutBaseLetters;
}
Unfortunately i am getting an error which confuses me:
不幸的是,我收到一个让我困惑的错误:
ERROR [org.springframework.scheduling.support.MethodInvokingRunnable] - Invocation of method 'setBaseLettersToAllWordsWithoutThem' on target class [class $Proxy48] failed
java.lang.IllegalArgumentException: Unable to resolve attribute [base_letters] against path
at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:118)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:223)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:194)
at pl.net.grodek.snd.dao.WordDaoImpl.getAllWordsWithoutBaseLetters(WordDaoImpl.java:95)
at pl.net.grodek.snd.service.WordServiceImpl.setBaseLettersToAllWordsWithoutThem(WordServiceImpl.java:175)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy48.setBaseLettersToAllWordsWithoutThem(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.support.MethodInvokingRunnable.run(MethodInvokingRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:51)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Please explain me what is wrong.
请向我解释什么是错的。
回答by kgautron
You need to use the name of the java attributeinstead of the name of the table column.
您需要使用java 属性的名称而不是表列的名称。
This applies to JPQL queries, as opposed to native queries(which are SQL-based).
这适用于JPQL 查询,而不是本机查询(基于 SQL)。
Assuming the attribute is called baseLetters
, you can replace with :
假设该属性被调用baseLetters
,您可以替换为:
c.select(words).where(cb.isNull(words.get("baseLetters")));