JPA Criteria Api选择列为空的对象

时间:2012-08-01 20:31:45

标签: java jpa criteria-api

我在PostgreSQL DB中有一个表“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 )
)

我有一个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;
    }

不幸的是我收到一个令我困惑的错误:

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.access$301(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)

请解释我的错误。

1 个答案:

答案 0 :(得分:12)

在编写JPA查询时(而不是本机查询),您需要使用java属性的名称,而不是表的列名。

假设该属性名为“baseLetters”,以下内容将完成此任务:

c.select(words).where(cb.isNull(words.get("baseLetters")));