Advertisement
Google Ad Slot: content-top
Spring Boot Named Queries
@NamedQuery allows defining reusable JPQL queries inside the entity class.
Example Entity Class
Let's assume we have a User entity with fields: id, name, email and age.
User.java
import jakarta.persistence.*;
@Entity
@NamedQueries({
// ✅ Find All Users
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
// ✅ Find User by Name
@NamedQuery(name = "User.findByName", query = "SELECT u FROM User u WHERE u.name = :name"),
// ✅ Find Users Older Than Age
@NamedQuery(name = "User.findOlderThan", query = "SELECT u FROM User u WHERE u.age > :age")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private int age;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Table name : user
| id | name | age | |
|---|---|---|---|
| 1 | John | john@gmail.com | 25 |
| 2 | Mike | mike@gmail.com | 35 |
| 3 | Watson | watson@gmail.com | 40 |
| 4 | Kenny | kenny@gmail.com | 45 |
Create JPA Repositories
UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> {
// ✅ Get All Users (Named Query)
List<User> findAll();
// ✅ Find User by Name (Named Query)
User findByName(@Param("name") String name);
// ✅ Find Users Older Than Age (Named Query)
List<User> findOlderThan(@Param("age") int age);
}
Create Services
UserService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
// ✅ Get All Users
public List<User> getAllUsers() {
return userRepository.findAll();
}
// ✅ Get User by Name
public User getUserByName(String name) {
return userRepository.findByName(name);
}
// ✅ Get Users Older Than Age
public List<User> getUsersOlderThan(int age) {
return userRepository.findOlderThan(age);
}
}
Create Controllers
UserController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
// ✅ Get All Users
@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
return ResponseEntity.ok(userService.getAllUsers());
}
// ✅ Get User by Name
@GetMapping("/{name}")
public ResponseEntity<User> getUserByName(@PathVariable String name) {
return ResponseEntity.ok(userService.getUserByName(name));
}
// ✅ Get Users Older Than Age
@GetMapping("/older/{age}")
public ResponseEntity<List<User>> getUsersOlderThan(@PathVariable int age) {
return ResponseEntity.ok(userService.getUsersOlderThan(age));
}
}
Testing the REST API with Postman:
Once the application is running, test the endpoints:
Get All Users GET:http://localhost:8080/users
Get User by Name GET:http://localhost:8080/users/{userName}
Get Users Older Than 30 GET:http://localhost:8080/users/older/{age}
Alternative: Named Native Queries (@NamedNativeQuery)
If you need raw SQL, use @NamedNativeQuery instead of @NamedQuery.
Define Named Native Queries
@NamedNativeQueries({
// ✅ Get All Users (Native SQL)
@NamedNativeQuery(name = "User.findAllNative", query = "SELECT * FROM users", resultClass = User.class),
// ✅ Find User by Name (Native SQL)
@NamedNativeQuery(name = "User.findByNameNative", query = "SELECT * FROM users WHERE name = :name", resultClass = User.class)
})
Repository Usage
public interface UserRepository extends JpaRepository<User, Long> {
@Query(name = "User.findAllNative", nativeQuery = true)
List<User> findAllUsersNative();
@Query(name = "User.findByNameNative", nativeQuery = true)
User findByNameNative(@Param("name") String name);
}