Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is a database view, and what are views good for in an application backed by Spring Data JPA? Can you insert or update through a view?

A view is a named, stored query: it holds no data of its own, and selecting from it simply runs that query against the base tables. Views hide joins, give consumers a stable shape while tables change, and limit which columns and rows someone can see; with Spring Data JPA we map a view to a read-only entity with Hibernate's @Immutable and an @Id on a unique column. Simple single-table views are updatable in PostgreSQL and MySQL, WITH CHECK OPTION stops writes that would fall outside the view, and more complex views need INSTEAD OF triggers.

Why this question is asked

Views are one of the oldest features in SQL, and they still solve everyday problems in Java backends: a screen that needs data from four tables, a report that should not see salary columns, or a table that must be split without breaking the code that reads it. Interviewers ask about them to check that we know what a view really is (and is not), how to use one from JPA, and the rules for writing through it. In this answer, we will define a view with an example, look at what views are good for, map one into Spring Data JPA, and then see when inserts and updates through a view work.

1What a View Is

A view is a query with a name. When we create it, the database stores only the query text, already parsed. When we select from the view, the database substitutes the query into ours and plans the combined statement as if we had written the join by hand. So a view never holds a copy of the data, and it always shows the current state of the base tables, the real tables underneath it.

SQL
CREATE VIEW customer_order_summary AS
SELECT c.id AS customer_id, c.name, count(o.id) AS order_count,
coalesce(sum(o.total), 0) AS total_spent
FROM customer c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;
SELECT * FROM customer_order_summary WHERE customer_id = 42;
-- customer_id | name | order_count | total_spent
-- 42 | Priya | 3 | 184.50
INSERT INTO orders (customer_id, total) VALUES (42, 10.00);
SELECT order_count FROM customer_order_summary WHERE customer_id = 42; -- 4, always current

Notice that the second query sees the new order immediately. That is the main difference from a materialized view, which stores the result and must be refreshed.

2What Views Are Good For

The first use is hiding complexity. Joins, calculations and naming conventions are written once in the view, and every query, report and repository method reuses them. The second is a stable interface: if we later split the customer table in two, we can redefine the view over the new tables and its readers notice nothing. The third is security. We can grant a role access to a view and not to its base tables, so a support tool sees name and email but not payment details, or only the rows of its own region.

In a Spring Data JPA application, a fourth use matters a lot: a view gives us a read model, a shape of data designed for one screen or API, that we can map like an entity without writing a long native query in the repository.

3Mapping a View in Spring Data JPA

To JPA, a view looks just like a table, so we can map it with @Entity and @Table. Two things need care. JPA requires an @Id, so we pick a column that is unique in the view, here customer_id. And since this view cannot be written to, we mark the entity with Hibernate's @Immutable, so Hibernate never tries to flush changes to it and skips dirty checking, which also saves work.

Java
@Entity
@Immutable // org.hibernate.annotations.Immutable
@Table(name = "customer_order_summary")
public class CustomerOrderSummary {
@Id private Long customerId; // unique per row of the view
private String name;
private long orderCount;
private BigDecimal totalSpent;
protected CustomerOrderSummary() { }
// getters only
}
interface SummaryRepository extends JpaRepository<CustomerOrderSummary, Long> {
List<CustomerOrderSummary> findByTotalSpentGreaterThan(BigDecimal amount);
}
summaryRepository.findById(42L).get().getOrderCount(); // 4

If the id is not really unique, Hibernate returns the same object for different rows, a confusing bug, so we choose the id carefully. If schema validation is enabled with ddl-auto=validate, Hibernate checks the view's columns like a table's. For very simple read needs we can skip the entity and use an interface projection or a DTO query instead; the view is worth it when several queries share the same shape.

4Can We Insert or Update Through a View?

Sometimes. PostgreSQL and MySQL make a view automatically updatable when each of its rows maps clearly to exactly one row of one base table. In practice that means a single table in FROM, and no aggregates, DISTINCT, GROUP BY, HAVING, window functions or set operations like UNION. Our summary view fails those rules because of GROUP BY. A view over one table with a filter passes them.

WITH CHECK OPTION adds a guard: the database rejects any insert or update through the view that would produce a row the view itself could not see. Without it, a user of the view below could set region = 'US' and the row would vanish from their view, a silent escape.

SQL
CREATE VIEW eu_customer AS
SELECT id, name, email, region FROM customer WHERE region = 'EU'
WITH CHECK OPTION;
UPDATE eu_customer SET email = 'p@example.com' WHERE id = 42; -- UPDATE 1
UPDATE eu_customer SET region = 'US' WHERE id = 42;
-- ERROR: new row violates check option for view "eu_customer"
UPDATE customer_order_summary SET name = 'X' WHERE customer_id = 42;
-- ERROR: cannot update view "customer_order_summary"
-- DETAIL: Views containing GROUP BY are not automatically updatable.
Is this view automatically updatable?
View shapePostgreSQLMySQL
One table, filter, column subsetyesyes
Contains GROUP BY, aggregates or DISTINCTno, use INSTEAD OF triggerno
Contains UNION or a window functionno, use INSTEAD OF triggerno
Joins several tablesno, use INSTEAD OF triggerpartly: one base table per statement
Simple views are updatable; anything that combines rows is read-only unless we add a trigger.

For views that are not automatically updatable, PostgreSQL lets us write an INSTEAD OF trigger on the view, a function that receives the attempted change and decides which base table rows to write; older code may use rules for the same purpose. MySQL has no INSTEAD OF triggers, so such views stay read-only there. In both databases, keeping writes on the base tables and using complex views only for reading is usually the clearest design.

5Follow-up questions

What does security_barrier or security_invoker do on PostgreSQL views?

security_barrier stops the planner from pushing a user's functions or conditions below the view's own filter, because a leaky function could otherwise see, and for example log, rows the view is meant to hide; use it when a view filters rows for security. security_invoker, available since PostgreSQL 15, makes the view check permissions and row-level security as the user running the query instead of the view's owner, which is the default. MySQL offers a similar choice with SQL SECURITY DEFINER or INVOKER on the view.

Does a view make a query slower?

Not by itself. PostgreSQL inlines the view's query into ours and plans the whole thing together, so filters usually reach the base tables. Views that contain aggregates, window functions or LIMIT can stop a filter from being pushed inside them, and deeply stacked views make plans harder to read, so we check EXPLAIN on hot paths.

Can Hibernate create the view for us?

Hibernate's schema generation creates tables, not views, so a mapped view entity would wrongly become a table with ddl-auto=create. We create views in Flyway or Liquibase migrations and use ddl-auto=validate or none. Hibernate's @Subselect is an alternative that maps an entity to a query string without a database view.

6Conclusion

In this answer, we saw that a view is a stored query that holds no data and always reflects the base tables. Views hide joins, give a stable interface while tables change, and restrict columns and rows for security. In Spring Data JPA we map a view as an entity with @Immutable and an @Id on a unique column, or use a projection. Simple single-table views are automatically updatable in PostgreSQL and MySQL, WITH CHECK OPTION blocks writes that would leave the view, and complex views need INSTEAD OF triggers in PostgreSQL or stay read-only.

What to say in the interviewAbout 90 seconds aloud

Open with

A view is a stored query with a name. It holds no data; when I select from it, the database expands the query into mine and plans them together, so it always shows the current data in the base tables.

Then, if they want more

I use views for three things: hiding joins and calculations so every reader uses the same logic, giving consumers a stable interface while I change the tables behind it, and security, granting access to a view that exposes only some columns or rows instead of the base tables.

In Spring Data JPA, I map a view like a table with @Entity and @Table, add Hibernate's @Immutable so it is never flushed or dirty checked, and put @Id on a column that is really unique in the view, otherwise Hibernate merges different rows. For a one-off query a projection is simpler.

As for writing: simple views over one table, without aggregates, DISTINCT, GROUP BY, window functions or UNION, are automatically updatable in both PostgreSQL and MySQL. WITH CHECK OPTION makes the database reject an insert or update that would create a row the view cannot see.

For other views, PostgreSQL supports INSTEAD OF triggers that translate the change into base table writes, but usually I keep writes on the tables and use complex views only to read.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a database view, and what are views good for in an application backed by Spring...": https://scrivio-interview-library.netlify.app/databases/stored-code/6fda6673d2/