Quick revision
The short answer
I would let teams use Hibernate-specific features where they solve real problems, such as batch fetching, natural ids and custom types, because in practice most companies never switch JPA provider and strict portability buys little. The rule I would set is to keep provider-specific code inside the persistence layer and document it, and to pay more attention to native SQL, because switching database engines happens more often than switching providers.
What to remember
In this answer, we saw that strict JPA portability has limited value because most companies never switch providers, while Hibernate-specific features like batch fetching, natural ids, filters and custom types solve real problems. The balanced rule is to allow them, keep them in the persistence layer and document them, and to focus portability effort on native SQL, since moving to another database engine is far more common than moving to another JPA provider.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would let teams use Hibernate-specific features where they solve real problems, such as batch fetching, natural ids and custom types, because in practice most companies never switch JPA provider and strict portability buys little. The rule I would set is to keep provider-specific code inside the persistence layer and document it, and to pay more attention to native SQL, because switching database engines happens more often than switching providers.
Why this question is asked
Every team using Spring Data JPA eventually meets a problem the specification does not solve well: N+1 queries on a collection, a column that needs a custom type, or a filter that every query should apply. Hibernate has an annotation for it, and someone asks whether we are allowed to use it. A rule set too strictly wastes time on workarounds; a rule set too loosely spreads provider details everywhere. Interviewers ask this leadership question to see how we reason about portability as a cost and a benefit rather than a principle. In this answer, we will look at how often provider switches really happen, what Hibernate features buy us, how to contain provider-specific code, why the database engine is the bigger portability question, and how to turn this into a team guideline.
1How Often Do Teams Really Switch Providers?
Jakarta Persistence is a specification, and Hibernate and EclipseLink are implementations of it. The promise of the specification is that code written only against jakarta.persistence could move from one implementation to another. That promise is real, but it is worth asking when we would ever use it.
In most companies the answer is almost never. Spring Boot uses Hibernate by default, nearly all Spring examples assume it, and switching providers changes subtle behaviour such as SQL generation, flush ordering and lazy loading, so it needs a full regression test even for perfectly portable code. Teams that do switch usually do it as part of a larger move, such as leaving an application server that bundled EclipseLink. So the value of staying strictly within the specification is an option we will probably never exercise, and the price is paid every day.
2What Hibernate-Specific Features Buy Us
Some Hibernate features solve problems that are expensive to solve any other way. @BatchSize on a collection makes Hibernate load the children of up to, say, 50 parents in one query instead of one query per parent, turning 51 queries into 2. @NaturalId maps a business key such as an ISBN or an account number and lets us load by it efficiently with caching. Custom types, through Hibernate's type system or JSON mapping, let us map a JSON column or a money type cleanly. @Filter applies a condition such as tenant_id = :tenant to every query on an entity, which is a common basis for multi-tenancy.
Doing the same with pure JPA means hand-written fetch queries in many places, repeated where clauses, or attribute converters that do not quite fit. Those workarounds cost more code, more bugs and often worse performance than one well-understood annotation.
3Containing Provider-Specific Code
The practical guideline is containment, not prohibition. Hibernate annotations on entity classes are already in the persistence layer, so they are fine. Code that unwraps the EntityManager into a Hibernate Session, or uses StatelessSession for bulk loads, belongs inside repository implementations, not in services or controllers. Service code should depend on repository interfaces and never import org.hibernate.
We document each use briefly: a comment on the annotation saying why it is there, and a short page in the team's guide listing the Hibernate features we use and why. If a switch ever happens, that list is the migration plan. A build rule, for example an ArchUnit test, can enforce that org.hibernate imports appear only in the persistence packages.
- Controllers and servicesNo
org.hibernateimports - Repository interfacesSpring Data, JPA types only
- Repository implementationsSession,
StatelessSessionallowed - Entity mappingsHibernate annotations allowed, documented
- DatabaseNative SQL reviewed separately
5 layers, from the top down. Step through them one at a time.
Containment keeps the cost of a future change small without banning useful features.
4The Database Engine Matters More
The portability question that actually comes up is the database. Companies move from Oracle to PostgreSQL to cut licence costs, or from a self-hosted MySQL to a managed cloud database. When that happens, Hibernate annotations are rarely the problem: Hibernate generates the right SQL for each dialect. The problem is native SQL: queries written with nativeQuery = true, stored procedures, database-specific functions such as Oracle's CONNECT BY or PostgreSQL's jsonb operators, and sequence or identity behaviour.
So I would review native SQL more carefully than Hibernate annotations. Each native query should have a reason (performance, a feature JPQL lacks), live in the repository layer and have an integration test that runs against the real database engine, for example with Testcontainers. That is where portability effort pays off.
5Turning It Into a Team Guideline
The guideline I would write for teams has three parts. Use the specification by default, since it covers most needs and is what people learn first. Use Hibernate-specific features when they solve a concrete problem, contained in the persistence layer and documented. Treat native SQL as the real portability risk: justify it, isolate it and test it against the actual engine.
I would explain the reasoning in the guideline rather than only the rule, because engineers follow rules better when they understand them. And I would revisit it if the business situation changes, such as a planned move to a platform with a different provider.
6Follow-up questions
Is there any situation where you would insist on pure JPA?
Yes, when portability is a real requirement, for example a product that customers deploy on their own Jakarta EE servers with whatever provider those servers ship. Then the specification is the contract, and provider-specific features need a fallback or must be avoided.
Do Hibernate features make upgrades harder?
Sometimes. Hibernate 6 changed its type system and some annotations, so custom types needed rework when moving from Hibernate 5. Keeping provider-specific code contained and documented makes those upgrades easier to find and test, which is another reason for the guideline.
How do you stop teams from overusing Hibernate features?
Code review and the documented list. Each new Hibernate-specific feature should come with a reason in the pull request, and the team guide shows the approved patterns. Most overuse comes from copying examples without understanding them, which explanation in the guide helps prevent.
7Conclusion
In this answer, we saw that strict JPA portability has limited value because most companies never switch providers, while Hibernate-specific features like batch fetching, natural ids, filters and custom types solve real problems. The balanced rule is to allow them, keep them in the persistence layer and document them, and to focus portability effort on native SQL, since moving to another database engine is far more common than moving to another JPA provider.
Open with
I would not restrict teams to the pure specification. In practice almost no company switches JPA provider; Spring Boot uses Hibernate, and even a switch with portable code needs a full regression test because SQL generation and flush behaviour differ.
Then, if they want more
So strict portability is an option we pay for every day and probably never use. Meanwhile Hibernate features solve real problems: @BatchSize turns an N+1 into two queries, @NaturalId handles business keys, filters help with multi-tenancy, and custom types map JSON or money columns cleanly. The workarounds in pure JPA cost more code and often perform worse.
What I would insist on is containment and documentation. Hibernate annotations on entities are fine, Session or StatelessSession only inside repository implementations, never in services, and I would enforce that with an ArchUnit rule. Each use gets a short reason in the code and in the team guide, so if we ever switch, that list is the plan.
The portability I would actually spend effort on is the database. Moving from Oracle to PostgreSQL is far more common than changing providers, and native queries and database-specific functions are what break, so I would isolate native SQL and test it against the real engine with Testcontainers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 5, Hibernate 6
- 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.
- Official documentation
- Jakarta EE specifications
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 "Should your teams restrict themselves to the JPA specification for portability, or use...": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/b51ab9e2d4/