2

Getting "No query defined for that name" when using entitymanager.

I have the following entity -


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

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

}

And using the Entity Manager as shown below -


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


    }

}

Below is the 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>

When I run Student_NamedQuery.java, I am getting "No query defined for that name" error. I searched all over the internet but failed to find out the problem.

1 Answer 1

2

You seem to have a typing error here. You have not used the correct name of the named query (LL != ll).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.