Java – JPA criteria API select object with null column

I have a table "word" in PostgreSQL DB:

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

I have a Dao method that must find the column 'base' in the table_ All the words of 'letters' I usually use spring My approach:

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 received a confused error:

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)

Please explain what my problem is

Solution

I think when using JPA queries (rather than native queries), you need to use the name of the Java attribute rather than the column name of the table Assuming that you respect the naming convention, this attribute is called "baseletters", and the following tasks should be performed:

c.select(words).where(cb.isNull(words.get("baseLetters")));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>