Programming & Development / April 18, 2025

Find Entities with null Fields in JPA (Spring Data)

Spring Data JPA null query findAllBy findFirstBy Optional custom query @RestController

Looking to fetch entities with null fields using Spring Data JPA? Whether you need all such entities or just the first one, it’s super clean with method naming conventions.

πŸ“Œ 1. Fetch All Where Field Is null

Suppose you have a User entity with an email field, and you want all users where email is null.

🧱 Entity Class

java

import javax.persistence.*;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    // Getters and Setters
}

πŸ“š Repository Interface

java

import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findAllByEmailIsNull();
}

πŸ”§ Service Example

java

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> getUsersWithNullEmail() {
        return userRepository.findAllByEmailIsNull();
    }
}

🌐 Controller Example

java

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/null-email")
    public List<User> getUsersWithNullEmail() {
        return userService.getUsersWithNullEmail();
    }
}

🎯 2. Fetch Just One (First Match)

Use findFirstBy...IsNull() for one result.

πŸ“š Repository Interface

java

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findFirstByEmailIsNull();
}

πŸ”§ Service

java

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public Optional<User> getFirstUserWithNullEmail() {
        return userRepository.findFirstByEmailIsNull();
    }
}

🌐 Controller

java

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/first-null-email")
    public Optional<User> getFirstUserWithNullEmail() {
        return userService.getFirstUserWithNullEmail();
    }
}

πŸ“ Summary

GoalMethod NameReturn TypeGet all where field is nullfindAllBy<Field>IsNull()List<T>Get one where field is nullfindFirstBy<Field>IsNull()Optional<T>

With Spring Data JPA’s query method naming, null checks are easyβ€”no need to write JPQL manually.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex