Spring Repository
No Entity
Java Spring
Spring Data
Programming Tutorial

Create a Spring repository without an entity

Codemia Knowledge Hub ·

A Spring Data JPA repository is built around an entity type and its identity, so creating one with no entity is usually the wrong abstraction. If your use case has no entity, write a custom repository class using JdbcTemplate or NamedParameterJdbcTemplate, or a query-oriented service that returns DTOs.

Why JPA repositories expect entities

A typical Spring Data repository looks like this:

java
public interface UserRepository extends JpaRepository<User, Long> {
}

The User entity is central. The repository API exists to persist and query entities or aggregate roots, not arbitrary result sets with no domain type behind them. Wanting JpaRepository<?, ?> with no entity usually means the design should change rather than be hacked around.

A JdbcTemplate repository for query-only access

For repository-style access to rows without a JPA entity, JdbcTemplate is usually the better fit:

java
1import org.springframework.jdbc.core.JdbcTemplate;
2import org.springframework.stereotype.Repository;
3
4import java.util.List;
5
6@Repository
7public class ReportRepository {
8
9    private final JdbcTemplate jdbcTemplate;
10
11    public ReportRepository(JdbcTemplate jdbcTemplate) {
12        this.jdbcTemplate = jdbcTemplate;
13    }
14
15    public List<String> findCustomerNames() {
16        return jdbcTemplate.query(
17            "select name from customers order by name",
18            (rs, rowNum) -> rs.getString("name")
19        );
20    }
21}

This suits data that is read-only or shaped for reporting rather than for entity lifecycle management.

Mapping results to DTOs

You can still map results into meaningful types instead of returning raw Object[] values:

java
public record CustomerSummary(long id, String name, int orderCount) {
}
java
1public List<CustomerSummary> findCustomerSummaries() {
2    return jdbcTemplate.query(
3        """
4        select c.id, c.name, count(o.id) as order_count
5        from customers c
6        left join orders o on o.customer_id = c.id
7        group by c.id, c.name
8        """,
9        (rs, rowNum) -> new CustomerSummary(
10            rs.getLong("id"),
11            rs.getString("name"),
12            rs.getInt("order_count")
13        )
14    );
15}

You get typed data without pretending the result set is a JPA-managed entity.

A repository-style interface without Spring Data

An interface still helps organize the code, even when Spring Data does not generate it:

java
public interface ReportQueries {
    List<CustomerSummary> findCustomerSummaries();
}

Provide your own implementation class. Be clear in naming whether a data-access class is JPA-backed or custom.

When you do need an entity

If the object:

  • is persisted over time
  • has identity
  • participates in updates and relationships

then it probably should be an entity, and avoiding one usually produces a worse design than modeling it properly. The real question is not "how do I make Spring Data ignore entities" but "is this an entity use case or a query use case". Choose the persistence abstraction from the shape of the problem, not from the word "repository".


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms