编程语言
首页 > 编程语言> > java – JPA Criteria Api select列为null的对象

java – JPA Criteria Api select列为null的对象

作者:互联网

我在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)

请解释我有什么问题.

解决方法:

我认为在使用JPA查询时(而不是本机查询),您需要使用java属性的名称,而不是表的列名.假设您尊重命名约定,该属性称为“baseLetters”,以下应该执行以下任务:

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

标签:java,jpa,criteria-api
来源: https://codeday.me/bug/20190715/1469529.html