Can you explain better shortly this transcript "Instructor: In this video, we're gonna learn
how to query objects.
Taking a look at our progress here of building a CRUD app,
we've covered the basics on how to read a single object,
here we're gonna learn how to query for multiple objects.
JPA has the JPA Query Language or JPQL.
It's a query language for retrieving objects.
It's similar in concept to SQL
where you can select from a given table
and grab some data accordingly.
You can also make use of constraints here
or selectors like where, like,
you can order the data join, so on and so forth.
However, the difference here is that JPQL
is based on the entity name and entity fields,
as opposed to the direct table names and table columns.
All right, so when you use JPQL,
by default you make use of the entity name and entity fields
and we'll see some examples of this coming up.
And here's an example of retrieving
all the students using JPQL.
I make use of this entityManager and I say createQuery,
and then I give FROM Student.
Now, this is the name of the actual JPA entity,
the class name, and then we give , Student.class.
This will make sure that this is a typed query,
and we make the assignment on the left hand side,
that's the query.
And one thing here on the query is that Student,
this is not the name of the database table.
All JPQL syntax is based on the entity name
and entity fields.
Now, once we have this query created,
then we can retrieve the data
by saying theQuery.getResultList
and it'll give us a list of students,
and then from there we can use it
in our application accordingly.
But the key here is that when using JPQL,
you make use of the entity name and also entity fields.
Let's take a look at an example
of retrieving students whose last name equals Doe.
Here I make use of entityManager, I say createQuery,
I give FROM Student WHERE lastName = Doe.
LastName is the field of the JPA entity,
that's the actual field name.
So we're not using the actual database column name,
we're using a JPA entity field name.
We can also retrieve students using predicates.
In this example, I say entityManager.createQuery
Assistant
FROM Student WHERE lastName = Doe OR firstName = Daffy.
And note here lastName and firstName,
those are the names of the actual JPA entity Fields.
We could also retrieve students using the like predicate.
In this example, I wanna get a list of all the students
whose email ends in luv2code.com.
Here I give FROM Student WHERE email LIKE,
I used a percent in this scenario to say let's match
on anything that ends with luv2code.com.
In the previous examples, we kinda hard coded
some of the data like, you know, lastName = Doe.
Well, you may want to use this as a parameter.
So maybe a user entered this into a web form
and you wanna read that information
and then search for that user's name accordingly.
Well, with JPQL we can make use of named parameters.
We have this method called FindByLastName.
This is a method that we're defining in our, maybe our DAO,
and then I have the last name being passed in.
I make use of entityManager.createQuery,
I give FROM Student WHERE lastName =,
and then I make use of this special syntax here,
I use :theData.
This is a JPQL named parameter, and these named parameters
are prefixed with a colon.
And you can kind of think of this as a placeholder
that we'll fill in later.
And then filling it in later on the next line of code,
we give theQuery.setParameter,
we provide the name of the parameter, theData,
and then we give the actual value, the lastName.
Now, let's talk about JPQL and the select clause.
The query examples I've shown you so far,
they did not really specify a select clause.
And that's because the Hibernate implementation
behind the scenes is lenient.
It allows for a Hibernate query language, HQL,
which is different than JPQL.
For strict JPQL, the select clause is required.
So really to follow the JPL standards strictly,
we'd have to write our query such
as select s FROM Student s.
Now ,this S here is really just a identification variable
or like an alias.
It basically gives us a reference
to the student entity that's being returned.
Now, this S, it can be any name.
I simply just used it just because that's a good,
you know, first initial for a student,
but you can use any name, any variable, whatever,
like a normal variable name or whatever or an alias.
This identification variable here,
it's useful for when you have complex queries,
when you need to refer to the entity later
and WHERE clauses and so on.
But, again, when you're making use of strict JPQL,
the select clause is required.
And then we saw some other examples
where we made use of queries and so forth for like,
you know, checking for a student that has a email
or checking for a student with a given last name.
This is how you would kind of revise or refactor
those queries using strict JPQL with a select clause.
Here in the first example,
I have select s FROM Student s WHERE s.email.
So again, S is a reference to the entity or the object,
so we know it's a student and we know that a student
has a field called email, so that's why we have s.email,
and then we give our normal LIKE like we had before.
Here at the bottom we have another example,
select s FROM Student s WHERE s.lastName equals
whatever the data parameter that we're gonna make use of.
Again, S is a reference to the entity
and then .lastName, that's an actual field
on the actual entity or the Java class.
Alright, so that's a good little piece of information here
regarding some of the strict JPQL
in regards to the select clause.
Alright, so this is really good stuff here.
Let's kind of step back
and take a look at our development process
of how we can add this into our DAO application.
Step one, we'll add this new method to the DAO interface.
Step two, we'll add a new method to the DAO implementation.
And then finally in step three,
we'll update the main application.
Let's take a look here at step number one
of adding a new method to the DAO interface.
Here's our student DAO, we have this new method here,
findAll, that's gonna return a list of students.
And then in step two, we define the DAO implementation.
We implement this method here, findAll.
We give entityManager.createQuery FROM Student,
remember that's the name of the JPA entity.
And notice here, there's no need
to add the transactional annotation
since we're doing a query, just kind of read only.
We're not making any updates
or modifications to the database.
And then finally in step three,
we update our main application.
In our command line runner, we call this method here,
queryForStudents, pass in the studentDAO,
and here's the method for the query for students.
We make use of our studentDAO.findAll,
that's a method that we just created in that previous slide.
We get back a list of students,
and then we simply display the list of students here,
just doing a for loop system.out.println(tempStudent).
Okay, this all looks really good.
Let's go ahead and move into the next video
and let's start writing some code."
Certainly! The transcript discusses how to query multiple objects using the Java Persistence API (JPA) and the Java Persistence Query Language (JPQL) in a Java application. Here's a concise explanation:
1. Introduction to JPQL
JPQL Overview: JPQL is a query language used to retrieve entities (objects) from a database in JPA. It is similar to SQL but operates on entity classes and their fields rather than directly on database tables and columns.
Entity-Based Queries: JPQL queries use the entity class names and field names defined in your Java code, making the code database-agnostic and more maintainable.
2. Basic Querying with JPQL
Retrieving All Entities:
java
TypedQuery<Student> query = entityManager.createQuery("FROM Student", Student.class);
List<Student> students = query.getResultList();
"FROM Student" refers to the Student entity class.
Student.class specifies the type of the result.
Note: The entity name (Student) and fields used in JPQL must match the Java class names and field names, not the database table or column names.
TypedQuery<Student> query = entityManager.createQuery(
"FROM Student WHERE lastName = 'Doe' OR firstName = 'Daffy'", Student.class);
Using LIKE Predicate:
java
TypedQuery<Student> query = entityManager.createQuery(
"FROM Student WHERE email LIKE '%luv2code.com'", Student.class);
Retrieves students whose email ends with luv2code.com.
4. Using Named Parameters
Parameterized Queries:
java
TypedQuery<Student> query = entityManager.createQuery(
"FROM Student WHERE lastName = :theData", Student.class);
query.setParameter("theData", lastName);
List<Student> students = query.getResultList();
:theData is a named parameter in JPQL.
setParameter binds the actual value to the named parameter.
Benefits:
Enhances security by preventing SQL injection.
Allows dynamic query execution based on user input.
5. Importance of the SELECT Clause
Strict JPQL Requires SELECT Clause:
While Hibernate's HQL may be lenient, standard JPQL requires specifying the SELECT clause.
Example with SELECT Clause:
java
TypedQuery<Student> query = entityManager.createQuery(
"SELECT s FROM Student s", Student.class);
s is an identification variable (alias) for the Student entity.
Useful for referencing the entity in the query, especially in complex queries.
Using Identification Variables in Conditions:
java
TypedQuery<Student> query = entityManager.createQuery(
"SELECT s FROM Student s WHERE s.email LIKE '%luv2code.com'", Student.class);
6. Implementing in a DAO Pattern
Step 1: Update DAO Interface
Add a method signature to find all students:
java
public List<Student> findAll();
Step 2: Implement DAO Method
Implement the findAll method using JPQL:
java
@Override
public List<Student> findAll() {
TypedQuery<Student> query = entityManager.createQuery("SELECT s FROM Student s", Student.class);
return query.getResultList();
}
Note: No transaction is required for read-only queries.
Step 3: Use in Main Application
Call the findAll method and process the results:
java
List<Student> students = studentDAO.findAll();
for (Student tempStudent : students) {
System.out.println(tempStudent);
}
Summary
JPQL allows querying of entities using entity class and field names.
Use SELECT clause and identification variables for clarity and compliance with JPQL standards.
Named parameters enhance security and flexibility in queries.
Implementing these queries involves updating DAO interfaces, implementations, and integrating them into the application logic.
Key Takeaways
Entity Focused: Always reference entity names and fields in JPQL, not database tables or columns.
JPQL Syntax: Similar to SQL but adapted for object-oriented entities.
Best Practices:
Use named parameters for dynamic queries.
Include the SELECT clause for standard compliance.
Use identification variables (aliases) for readability and reference.
By understanding these concepts, you can effectively query multiple objects in a JPA application using JPQL, making your data access layer robust and flexible.