In the previous article, we discussed how to create a database query using a method name. In this article, we will learn to create database queries by using named queries.
We can specify named queries with Spring Data JPA by using a properties file, annotations, or
the orm.xml
file. In this article, we will focus on creating named queries using @NamedQuery
and @NamedQueries
annotations.
Let's develop a complete example to demonstrate the usage of @NamedQuery
and
@NamedQueries
using the Spring Boot application which
quickly bootstraps with autoconfiguration.
There are many ways to create a Spring Boot application. You can refer below articles to create a Spring Boot application.
>> Create Spring Boot Project With Spring InitializerHere is the pom.xml
file for your reference:
The Spring Boot Maven plugin provides many convenient features:
main()
method to flag as a
runnable class.In this example, we store User
objects, annotated as a JPA entity.
import java.util.Date;
import jakarta.persistence.*;
@Entity
@Table(name = "users")
@NamedQuery(name = "User.findByEmailAddress", query = "select u from User u where u.emailAddress = ?1")
@NamedQueries(value = {
@NamedQuery(name = "User.findByLastname", query = "select u from User u where u.lastname = ?1")
})
public class User {
private long id;
private String firstname;
private String lastname;
private Date startDate;
private String emailAddress;
private int age;
private int active;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getActive() {
return active;
}
public void setActive(int active) {
this.active = active;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@Override
public String toString() {
return "User [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", startDate=" + startDate +
", emailAddress=" + emailAddress + ", age=" + age + ", active=" + active + "]";
}
}
User
class is annotated with @Entity
, indicating that it is a JPA entity.@Id
so that JPA will recognize
it as the object’s ID. The id
property is also annotated with @GeneratedValue
to indicate that the ID
should be generated
automatically.Specifies a static, named query in the Java Persistence query language. Query
names are scoped to the
persistence unit. The NamedQuery
annotation can be applied to an entity or
mapped superclass.
@Entity
@Table(name = "users")
@NamedQuery(name = "User.findByEmailAddress", query = "select u from User u where u.emailAddress = ?1")
public class User {
}
Specifies multiple named Java Persistence query language queries. Query names are scoped to the persistence
unit. The NamedQueries
annotation can be applied to an entity or mapped
superclass.
@Entity
@Table(name = "users")
@NamedQueries(value = {
@NamedQuery(name = "User.findByLastname", query = "select u from User u where u.lastname = ?1") })
public class User {
}
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.guides.springboot2.springboottestingexamples.model.User;
/**
* UserRepository demonstrates the method name query generation.
*
* @author Ramesh Fadatare
*
*/
@Repository
public interface UserRepository extends JpaRepository < User, Long > {
User findByEmailAddress(String emailAddress);
List < User > findByLastname(String lastname);
}
Here you create an Application
class with all the components.
package net.guides.springboot2.springboottestingexamples;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import net.guides.springboot2.springboottestingexamples.model.User;
import net.guides.springboot2.springboottestingexamples.repository.UserRepository;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private UserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String...args) throws Exception {
User user = new User();
user.setActive(1);
user.setAge(28);
user.setEmailAddress("ramesh24@gmail.com");
user.setFirstname("Ramesh");
user.setLastname("Fadatare");
user.setStartDate(new Date());
user = userRepository.save(user);
System.out.println("-------------------------------------:: " + user.getId());
System.out.println(" ---------------@NamedQuery ---------------------");
System.out.println("--------------findByEmailAddress -----------------");
User user2 = userRepository.findByEmailAddress("ramesh24@gmail.com");
System.out.println(user2.toString());
System.out.println(" ---------------@NamedQuery ---------------------");
System.out.println("--------------findByLastname -----------------");
List < User > user3 = userRepository.findByLastname("Fadatare");
System.out.println(user3.get(0).toString());
}
}
Note that we have used UserRepository
methods to demonstrate @NamedQuery
and
@NamedQueries
annotations.
We are using Maven so we can run the application using ./mvnw spring-boot:run. Or you can build the JAR file with ./mvnw clean package. Then you can run the JAR file:
java -jar target/spring-data-jpa-example-0.1.0.jar