java.lang.IllegalArgumentException:没有为该名称定义查询

时间:2019-06-02 22:24:09

标签: java hibernate jpa

使用entitymanager时获取“没有为该名称定义查询”。

我有以下实体-


@Entity 
@Table(name="STUDENT")
@NamedQuery(name="student.getAll", query="select s from Student s")
public class Student {

    // ...........

}

并使用实体管理器,如下所示-


public class Student_NamedQuery {

    public static void main(String[] args) {

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.udemy.jpa");

        EntityManager entityManager = entityManagerFactory.createEntityManager();

        entityManager.getTransaction().begin();


        Query query = entityManager.createNamedQuery("student.getALL");


        List<Student> studentList = query.getResultList();

        System.out.println(studentList);

        entityManager.close();
        entityManagerFactory.close();


    }

}

下面是persistence.xml-


<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="com.udemy.jpa" transaction-type="RESOURCE_LOCAL">  

        <class>com.udemy.jpa.Student</class>

            <properties>
            <property name="hibernate.archive.autodetection" value="class, hbm" />
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/udemy?serverTimezone=UTC" />
            <property name="hibernate.connection.user" value="root" />
            <property name="hibernate.connection.password" value="root" />
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="use_sql_comments" value="true" />

        </properties>
    </persistence-unit>
</persistence>

运行Student_NamedQuery.java时,出现“没有为该名称定义查询”错误。我在互联网上进行了搜索,但未能找到问题所在。

1 个答案:

答案 0 :(得分:1)

您似乎在这里输入错误。您没有使用命名查询的正确名称(LL!= ll)。