微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JPQL-嵌套的异常是org.springframework.dao.InvalidDataAccessApiUsageException

如何解决JPQL-嵌套的异常是org.springframework.dao.InvalidDataAccessApiUsageException

我是SpringBoot / Spring和JPQL(Hibernate的新手),我不明白为什么会收到此错误? 怎么了?

2020-09-24 07:26:28.736 ERROR 6036 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing Failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: java.lang.Arrayindexoutofboundsexception: 0; nested exception is java.lang.IllegalArgumentException: java.lang.Arrayindexoutofboundsexception: 0] with root cause

java.lang.Arrayindexoutofboundsexception: 0
    at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:959) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createNativeQuery(AbstractSharedSessionContract.java:918) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.buildQueryFromName(AbstractSharedSessionContract.java:896) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:997) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.internal.AbstractSessionImpl.createNamedQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at com.sun.proxy.$Proxy88.createNamedQuery(UnkNown Source) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at com.sun.proxy.$Proxy88.createNamedQuery(UnkNown Source) ~[na:na]
    at com.crsardar.java.spring.boot.junk.PersonRepo.getPersons(PersonRepo.java:26) ~[classes/:na]
    at com.crsardar.java.spring.boot.junk.PersonRepo$$FastClassBySpringcglib$$d770c807.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at 

我的代码如下-pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-boot-rest</artifactId>
            <groupId>com.crsardar.java.spring.boot.rest</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
        <artifactId>spring-boot-jpa</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-rest-jpa</name>
        <description>Hands on SpringBoot's JPA</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <!-- This is to compare JDBC with JPA,this is NOT required for only JPA usages -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jdbc</artifactId>
            </dependency>
            <!-- In memory DB for testing only-->
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!-- Extras -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

控制器

    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/junk")
    public class PersonController
    {
        int count = 0;
    
        @Autowired
        private PersonRepo personRepo;
    
        @GetMapping("/persons")
        public List<Person> getPersons()
        {
            count++;
    
            Person person = new Person("Person " + count,"City " + count);
            personRepo.createPerson(person);
    
           return personRepo.getPersons();
        }
    }

存储库

    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.TypedQuery;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    
    @Repository
    @Transactional
    public class PersonRepo
    {
        @Autowired
        private EntityManager entityManager;
    
        public void createPerson(final Person person)
        {
            entityManager.persist(person);
        }
    
        public List<Person> getPersons()
        {
            TypedQuery<Person> allPersons = entityManager.createNamedQuery("get_all_persons",Person.class);
            return allPersons.getResultList();
        }
    }

实体

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.NamednativeQuery;
    
    @Entity
    @NamednativeQuery(name = "get_all_persons",query = "SELECT p FROM Person p")
    public class Person
    {
        @Id
        @GeneratedValue
        private Long id;
        private String name;
        private String city;
    
        public Person()
        {
        }
    
        public Person(String name,String city)
        {
            this.name = name;
            this.city = city;
        }
    
        public Long getId()
        {
            return id;
        }
        public void setId(Long id)
        {
            this.id = id;
        }
    
        public String getName()
        {
            return name;
        }
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getCity()
        {
            return city;
        }
        public void setCity(String city)
        {
            this.city = city;
        }
    
        @Override
        public String toString()
        {
            return "Person{" + "id=" + id + ",name='" + name + '\'' + ",city='" + city + '\'' + '}';
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。