gpt4 book ai didi

java.lang.IllegalArgumentException : No query defined for that name

转载 作者:行者123 更新时间:2023-11-30 05:36:52 28 4
gpt4 key购买 nike

使用实体管理器时出现“没有为该名称定义查询”。

我有以下实体 -


@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 时,出现“没有为该名称定义查询”错误。我在网上查遍了也没找到问题所在。

最佳答案

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

关于java.lang.IllegalArgumentException : No query defined for that name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56419368/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com