byAuthor(String author). You never write the class that implements it. Somebody else does — and the interesting question is who, when, and what happens when you misspell a field name.
That question is the real difference between Spring Data JPA, which the Java world has used for over a decade, and Jakarta Data, the Jakarta EE 11 specification whose Hibernate implementation (Hibernate Data Repositories) is now good enough to try. Spring Data builds the implementation when your application starts. Hibernate builds it while javac runs, and refuses to compile if a name or a query is wrong. This article runs both against the same Book table, side by side, and shows the real SQL, the real errors and the real output.
The companion project is asmhatre/hibernate8-jpa4-demo/jakarta-data. Its tests write the transcripts in output/, and every console block below is quoted from one of those files. If a claim here stops being true, the build goes red.
Versions, and what is and is not GA (checked 2026-11-26). The idea behind this article was titled “Jakarta Data 1.1 with Hibernate 8”. Neither is GA today, so the article is written against what actually exists:
GA: Hibernate ORM 7.4.17.Final (the newest.Finalin Maven Central’smaven-metadata.xml; hibernate.org lists it as the latest stable series, released 2026-11-19) with Jakarta Data API 1.0.2. Spring Boot 4.2.0 and Spring Data JPA 4.2.0 (Boot manages Hibernate 7.4.15.Final underneath).
Pre-release: Hibernate ORM 8.0.0.CR2 (a release candidate, published 2026-11-24; hibernate.org lists the 8.0 series as “Development”) with Jakarta Data API 1.1.0-M4 (a milestone — the Jakarta EE specification page lists Jakarta Data 1.1 as “under development”) and Jakarta Persistence 4.0.0-M8 (a milestone).@Select,@Isand@Firstexist only in this pre-release line; I checked the 1.0.2 and 1.1.0-M4 jars, and they are absent from 1.0.2.
JDK Temurin 25.0.5.1 (current LTS), H2 2.5.252, JUnit 6.1.3, Maven 3.9.11. Every section says which of the two lines it ran on; the module runs both withmvn testandmvn -Ph8 test.
A repository is an interface that someone else implements
If you have never used either library, the idea is small. A repository is the one place your code asks “give me the books by this author”, so the rest of the application never writes a query. You declare what you want as an interface method, and a framework supplies the body. The part that matters for everything below is when the body is supplied. Spring Data JPA reads your interface at runtime, when the application context starts, and creates a proxy object. Jakarta Data is a specification that leaves the choice to the implementation, and Hibernate chose to generate ordinary Java source during compilation. The diagram shows the two paths.javac (Hibernate’s hibernate-processor is an annotation processor), so a bad name never reaches a .class file. In the Spring lane the check lives in the proxy factory, so it runs when the context starts. The section on typos below shows the real messages from both. Here is the Jakarta Data interface the rest of the article uses; the full file is hibernate-data/src/main/java/com/ankurm/data/Library.java.
@Repository
public interface Library {
/** The "resource accessor": the generated class passes the StatelessSession it was constructed with. */
StatelessSession session();
@Insert
void add(Book book);
@Save
void save(Book book);
@Delete
void remove(Book book);
// ---- @Find: the parameter names ARE the query ----
@Find
Optional<Book> byIsbn(String isbn);
@Find
@OrderBy("publishedYear")
List<Book> byAuthor(String author);
@Find
List<Book> byAuthorAndPublishedYear(String author, int publishedYear);
Three annotations carry all the meaning here. @Repository marks the interface for processing. @Find means “generate a lookup whose conditions are my parameters”. session() is the resource accessor: an abstract method that returns the Hibernate StatelessSession the generated class works through. The generated class receives that session in its constructor, and the accessor just hands it back.
Going deeper: what the specification says versus what Hibernate adds
Jakarta Data 1.0 (part of Jakarta EE 11) standardises the annotations (@Repository, @Find, @Query, @Insert, @Update, @Delete, @Save, @OrderBy, @By, @Param) and the base interfaces. It deliberately does not say how an implementation produces the body. Hibernate’s answer, per its Hibernate Data Repositories documentation, is the processor plus a StatelessSession; the same page states that @Query HQL is checked for syntax and fully type-checked at compile time, and that @SQL native queries are not validated.
Spring Data JPA, by contrast, is not a specification implementation at all. It predates Jakarta Data and has its own repository hierarchy (JpaRepository, ListCrudRepository) and its own query language embedded in method names. Both can coexist in one application because they only share the entity classes.
- Hibernate Data Repositories reference: docs.hibernate.org/orm/8.0/repositories
- Jakarta Data specification page: jakarta.ee/specifications/data
- The processor setup in this module: hibernate-data/pom.xml
The smallest repository that works
There is no Spring and no application server in the first demo. A test class boots Hibernate on in-memory H2, seeds six books, and calls the repository. The helper that does the booting is hibernate-data/src/test/java/com/ankurm/data/Lab.java; it also installs aStatementInspector so every SQL string Hibernate prepares is recorded and printed next to the result.
The entity is a plain JPA class (hibernate-data/src/main/java/com/ankurm/data/Book.java). The three lookups from the interface above ran like this on the GA line (Hibernate 7.4.17.Final, Jakarta Data 1.0.2); the full transcript is output/ga/01-find-by-parameter-name.txt.
byIsbn("333") -> Book[333 'Java Concurrency in Practice' Brian Goetz 2006 52.00]
byAuthor("Joshua Bloch") @OrderBy -> [Effective Java/2001, Java Puzzlers/2005]
byAuthorAndPublishedYear(Bloch,2005) -> [Java Puzzlers]
SQL Hibernate prepared:
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.isbn=?
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.author=? order by b1_0.publishedYear
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.author=? and b1_0.publishedYear=?
Read the output as an experiment on the one rule of @Find: the parameter name is the query. A parameter called author becomes “where author = ?”. Two parameters become “and”. @OrderBy("publishedYear") becomes the order by in the second statement. There is no query string anywhere, yet the SQL is exactly what you would have written.
The code that produced it is not magic either. This is the generated method for byAuthorAndPublishedYear, copied from the class the processor wrote on the pre-release line (output/h8/generated-_Library.java.txt):
public List<Book> byAuthorAndPublishedYear(String author, int publishedYear) {
var _builder = session.getCriteriaBuilder();
var _query = _builder.createQuery(Book.class);
var _entity = _query.from(Book.class);
_query.where(
author==null
? _entity.get(Book_.author).isNull()
: _builder.equal(_entity.get(Book_.author), author),
_builder.equal(_entity.get(Book_.publishedYear), publishedYear)
);
try {
var _select = session.createSelectionQuery(_query);
return _select.getResultList();
}
catch (PersistenceException _ex) {
throw new DataException(_ex.getMessage(), _ex);
}
}
It is a Criteria query built from your parameters, with one detail worth noticing: a null argument becomes is null, not = null. When a database column holds nulls this is what you want; when a null sneaks in by accident you silently get the wrong rows, so validate arguments at your own boundary.
The generated class has a different name on the two lines. On Hibernate 7.4.17 the implementation isLibrary_; on 8.0.0.CR2 it is_Library, andLibrary_becomes the Jakarta Persistence 4 static metamodel of the repository. The test printed it (output/ga/00-versions.txt and output/h8/00-versions.txt):com.ankurm.data.Library_versuscom.ankurm.data._Library. Application code rarely names it, because CDI or Spring injection supplies it, but a hand-writtennew Library_(session)compiles on one line and not on the other. The Hibernate 8 documentation writes_Library, which matches 8.0, not 7.4.
Going deeper: lifecycle methods, return types and exceptions
@Insert, @Update, @Delete and @Save map to StatelessSession operations: insert, update, delete and an upsert. In the generated save method of the GA line, an entity whose identifier is null is inserted and anything else is upserted. A unique-constraint violation on insert is translated to Jakarta Data’s EntityExistsException, other persistence failures to DataException, and stale-state failures to OptimisticLockingFailureException. A @Find method may return the entity, an Optional or a List (all three ran above); the Hibernate documentation also allows a nullable return.
- Generated GA implementation, every method: output/ga/generated-Library_.java.txt
- Generated pre-release implementation: output/h8/generated-_Library.java.txt
- The exception mapping is in the
catchblocks of the generatedadd,saveandremovemethods in the file above
The same repository in Spring Data JPA
Spring Data JPA expresses the same idea in a different grammar: the method name is the query.findByAuthorAndPublishedYear is parsed into “author equals, and published year equals” when the context starts. Here is the Spring repository for the same table (spring-data/src/main/java/com/ankurm/sd/BookRepository.java):
Optional<Book> findByIsbn(String isbn);
List<Book> findByAuthorOrderByPublishedYear(String author);
List<Book> findByAuthorAndPublishedYear(String author, int publishedYear);
long countByAuthor(String author);
List<Book> findTop2ByPublishedYearGreaterThanEqualOrderByPriceDesc(int since);
// ---- @Query: JPQL kept in a string, validated when the application context starts ----
@Query("select b from Book b where b.price < :max order by b.price")
List<Book> cheaperThan(@Param("max") BigDecimal max);
It ran on Spring Boot 4.2.0 with Spring Data JPA 4.2.0 (GA). The transcript is output/spring/01-derived-queries.txt; the SQL lines are the proof that both libraries ask the database the same question.
findByIsbn("333") -> Book[333 'Java Concurrency in Practice' Brian Goetz 2006 52.00]
findByAuthorOrderByPublishedYear(Bloch) -> [Effective Java/2001, Java Puzzlers/2005]
findByAuthorAndPublishedYear(Bloch,2005) -> [Java Puzzlers]
findTop2ByPublishedYearGreaterThanEqualOrderByPriceDesc(2000) -> [Core Java 60.00, Java Concurrency in Practice 52.00]
SQL Hibernate prepared:
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.isbn=?
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.author=? order by b1_0.published_year
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.author=? and b1_0.published_year=?
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.published_year>=? order by b1_0.price desc fetch first ? rows only
Two things differ, and neither is about correctness. First, the table in Spring’s output is named book with a column published_year, while Hibernate used Book and publishedYear; that is Spring Boot’s default naming strategy, not Spring Data, and it explains why the two modules print different column names for the same entity. Second, findTop2… encodes a limit and a sort in a 55-character method name. The Jakarta Data equivalent keeps the name short and moves the intent into annotations, which is what the next section shows with @First.
A side-by-side summary of the grammar, taken from the two repository files:
| You want | Jakarta Data + Hibernate | Spring Data JPA |
|---|---|---|
| Lookup by one field | @Find Optional<Book> byIsbn(String isbn) | Optional<Book> findByIsbn(String isbn) |
| Two conditions | @Find … byAuthorAndPublishedYear(String author, int publishedYear) | findByAuthorAndPublishedYear(…) |
| Sort | @OrderBy("publishedYear") | …OrderByPublishedYear in the name |
| Hand-written query | @Query("where price < :max order by price") (HQL, may omit select and from) | @Query("select b from Book b where b.price < :max order by b.price") |
| Who builds the body | javac, via an annotation processor | a runtime proxy at context start |
Going deeper: why Spring Data can be a smaller dependency story, and when it cannot
The Jakarta Data module needs jakarta.data-api, hibernate-core and, only at build time, hibernate-processor. The Spring module needs spring-boot-starter-data-jpa and nothing at build time. Outside a container, the Jakarta Data repository needs you to open and close the StatelessSession yourself; the Hibernate documentation says generated code creates and destroys it under Jakarta EE, Quarkus injects one, and in other environments you manage it. Spring Data hides that behind @Transactional and the entity manager proxy.
When the Hibernate processor sees Spring on the class path it adds @Component and uses an ObjectProvider<StatelessSession>, per the same documentation page. I did not run that integration here: the Spring module in this repository uses Spring Data only.
- Spring Data JPA pagination in depth: Page vs Slice vs Keyset Scrolling
- Both modules’ build files: hibernate-data/pom.xml, spring-data/pom.xml
Choosing the columns: @Query, records and @Select
Loading a whole Book when you only want a title for a dropdown wastes columns. A projection is a query that returns only some of them. Both libraries support this, and the output shows the saving in SQL rather than in a claim.
In Jakarta Data you can write the HQL yourself with @Query and return a record; the select list is matched to the record’s components by position. The record is hibernate-data/src/main/java/com/ankurm/data/TitleAndYear.java and the query is in hibernate-data/src/main/java/com/ankurm/data/Library.java:
@Query("where price < :max order by price")
List<Book> cheaperThan(BigDecimal max);
/** A projection through a record: positional match between select list and record components. */
@Query("select title, publishedYear from Book where author = :author order by publishedYear")
List<TitleAndYear> titlesBy(String author);
@Query("select count(*) from Book where author = :author")
long countBy(String author);
GA line, transcript output/ga/02-query-and-record-projection.txt:
cheaperThan(46.00) -> [Java in a Nutshell 30.00, Java Puzzlers 39.50, Effective Java 45.00]
titlesBy(Bloch) -> [TitleAndYear[title=Effective Java, publishedYear=2001], TitleAndYear[title=Java Puzzlers, publishedYear=2005]]
countBy(Bloch) -> 2
SQL Hibernate prepared:
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.price<? order by b1_0.price
sql> select b1_0.title,b1_0.publishedYear from Book b1_0 where b1_0.author=? order by b1_0.publishedYear
sql> select count(*) from Book b1_0 where b1_0.author=?
Look at the second SQL line: select b1_0.title,b1_0.publishedYear. Two columns left the database, not six. Spring Data does the same when the return type is a class-based projection (a record), and one column when it is an interface projection. The relevant methods are findByAuthorOrderByPublishedYearAsc and the generic findByAuthorOrderByTitle(String, Class<T>) in spring-data/src/main/java/com/ankurm/sd/BookRepository.java, and the output is output/spring/02-query-and-projections.txt:
cheaperThan(46.00) -> [Java in a Nutshell 30.00, Java Puzzlers 39.50, Effective Java 45.00]
findByAuthorOrderByPublishedYearAsc -> [TitleAndYear[title=Effective Java, publishedYear=2001], TitleAndYear[title=Java Puzzlers, publishedYear=2005]]
findByAuthorOrderByTitle(TitleOnly) -> [Effective Java, Java Puzzlers]
findByAuthorOrderByTitle(Book) -> [Effective Java, Java Puzzlers]
countByAuthor(Bloch) -> 2
SQL Hibernate prepared:
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.price<? order by b1_0.price
sql> select b1_0.title,b1_0.published_year from book b1_0 where b1_0.author=? order by b1_0.published_year
sql> select b1_0.title from book b1_0 where b1_0.author=? order by b1_0.title
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.author=? order by b1_0.title
sql> select count(b1_0.id) from book b1_0 where b1_0.author=?
In Spring the return type picks the columns: the TitleAndYear record gives two, the TitleOnly interface (spring-data/src/main/java/com/ankurm/sd/TitleOnly.java) gives one, and asking for Book.class gives all six. That is flexible and slightly invisible; you have to look at the call site to know what is fetched.
@Select, @Is and @First (pre-release only)
Everything in this subsection runs on Hibernate 8.0.0.CR2 with Jakarta Data 1.1.0-M4, both pre-release. The three annotations are not inJakarta Data 1.1 adds ways to avoid writing HQL even for projections and comparisons.jakarta.data-api1.0.2, and the Jakarta EE specification page lists Jakarta Data 1.1 as “under development”. Names, attributes and behaviour may change before a final release. The code is compiled only undermvn -Ph8.
@Select names the attribute to return, @Is turns a plain parameter into a comparison such as “at least”, and @First(n) limits the result. The repository is hibernate-data/src/h8/java/com/ankurm/data/Catalog.java:
@Find(Book.class)
@Select("title")
@OrderBy("title")
List<String> titlesBy(String author);
/** A record return type with @Find: components are matched to entity attributes by name. */
@Find(Book.class)
@OrderBy("publishedYear")
List<TitleAndYear> slimBooksBy(String author);
/** @Is turns a plain parameter into a comparison. */
@Find
@OrderBy("price")
List<Book> pricedAtLeast(@By("price") @Is(AtLeast.class) BigDecimal min);
@Find
@OrderBy("price")
List<Book> youngerAndCheaper(@By("publishedYear") @Is(AtLeast.class) int since,
@By("price") @Is(LessThan.class) BigDecimal below);
/** @First(n) is a limit without a PageRequest parameter. */
@Find
@OrderBy(value = "price", descending = true)
@First(2)
List<Book> twoMostExpensive(@By("publishedYear") @Is(AtLeast.class) int since);
Transcript output/h8/04-select-is-first.txt:
titlesBy(Bloch) @Select(title) -> [Effective Java, Java Puzzlers]
slimBooksBy(Bloch) record return -> [TitleAndYear[title=Effective Java, publishedYear=2001], TitleAndYear[title=Java Puzzlers, publishedYear=2005]]
pricedAtLeast(48.00) @Is(AtLeast) -> [Modern Java in Action 48.00, Java Concurrency in Practice 52.00, Core Java 60.00]
youngerAndCheaper(2018, 50.00) -> [Java in a Nutshell 30.00, Modern Java in Action 48.00]
twoMostExpensive(2000) @First(2) -> [Core Java 60.00, Java Concurrency in Practice 52.00]
SQL Hibernate prepared:
sql> select b1_0.title from Book b1_0 where b1_0.author=? order by 1
sql> select b1_0.title,b1_0.publishedYear from Book b1_0 where b1_0.author=? order by 2
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.price>=? order by b1_0.price
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.publishedYear>=? and b1_0.price<? order by b1_0.price
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.publishedYear>=? order by b1_0.price desc fetch first ? rows only
Compare the last line with Spring’s findTop2ByPublishedYearGreaterThanEqualOrderByPriceDesc: same SQL (order by … desc fetch first ? rows only), but the intent is spread across annotations rather than packed into a name. Neither is objectively better; the annotation form scales better as the condition count grows, and the name form is quicker to write when it is short.
Going deeper: what @Is can compare, and restrictions
The jakarta.data.constraint package in 1.1.0-M4 contains AtLeast, AtMost, Between, EqualTo, GreaterThan, LessThan, In, Like, Null, NotNull and their negations (I listed the jar). The same release also contains a jakarta.data.restrict package with Restriction and Restrict, which lets a repository method accept a condition object built at run time. The Hibernate 8.0 documentation says restrictions “will be standardized by Jakarta Data 1.1” and, until then, recommends Hibernate’s own Restriction API; I did not exercise that part.
- The two repository interfaces compared: hibernate-data/src/main/java/com/ankurm/data/Library.java (GA) and hibernate-data/src/h8/java/com/ankurm/data/Catalog.java (pre-release)
- Generated code for
@Select: output/h8/generated-_Catalog.java.txt
Where a typo shows up: compile time versus context start
This is the experiment the article exists for. Take four mistakes a person actually makes: a misspelled field name, a parameter of the wrong type, a bad attribute in@OrderBy, and a bad attribute inside HQL. Put each in its own file and see what happens. The files are in hibernate-data/src/broken and are excluded from the normal build; scripts/broken.sh copies the module, drops one file in, runs mvn compile and keeps the compiler’s own message.
[from Book where prise < :max] suffix (output/ga/05-compile-errors.txt):
##### BadHql
com/ankurm/data/BadHql.java:[15,12] Could not interpret path expression 'prise' [from Book where prise < :max]
##### BadOrderBy
com/ankurm/data/BadOrderBy.java:[16,5] no matching field named 'year' in entity class 'com.ankurm.data.Book'
##### MisspelledParameter
com/ankurm/data/MisspelledParameter.java:[15,32] no matching field named 'autor' in entity class 'com.ankurm.data.Book'
##### WrongParameterType
com/ankurm/data/WrongParameterType.java:[15,39] matching field has type 'java.lang.Integer' in entity class 'com.ankurm.data.Book'
The Spring Data equivalent of the first and fourth mistakes lives in spring-data/src/test/java/com/ankurm/broken/derived/BrokenRepository.java (findByAutor) and spring-data/src/test/java/com/ankurm/broken/query/BrokenQueryRepository.java (b.prise). They compile without complaint, because to javac they are just an interface and a string. The failure arrives when the context starts (output/spring/04-startup-failures.txt):
BrokenDerivedConfig -> PropertyReferenceException: No property 'autor' found for type 'Book'; Did you mean 'author'
BrokenQueryConfig -> PathElementException: Could not resolve attribute 'prise' of 'com.ankurm.sd.Book'
Spring’s check is early, but it is not a compile error. The application refuses to start, so the mistake cannot reach a user, but it does need something to start the application: a test that boots the context, or a developer who runs the app. A module with no context-loading test can build, pass its unit tests and fail on deploy. Hibernate’s processor removes that step. In both cases@Querytext is checked: JPQL by Hibernate at context start in Spring, HQL by the processor at compile time in Jakarta Data. I did not test Spring Data’s ahead-of-time repository processing (theprocessAotbuild step), which may move some of Spring’s checking earlier; I am not claiming either way.
Going deeper: what the processor can and cannot check
Per the Hibernate documentation, the processor reports mismatched parameter names or types in automatic queries, validates @OrderBy content, and fully type-checks @Query HQL, but does not validate @SQL native queries. The first two bullets are what the files above exercise. What it cannot know is the database: a column that exists in the entity but not in the actual schema is only found at run time. It also cannot know your data: byIsbn returning Optional will throw if two rows share an ISBN, which the unique = true on Book.isbn prevents here.
- The driver script: scripts/broken.sh
- The four broken sources: hibernate-data/src/broken/
- A related ankurm.com article on newer Hibernate behaviour: Hibernate 8 graph-based flush ordering
Stateless by default, stateful when you ask
The biggest behavioural difference is not the annotations; it is what happens to an entity after the repository hands it to you. A HibernateStatelessSession keeps no persistence context. The entity it returns is just an object, and changing it does nothing. Spring Data JPA works through an EntityManager, so inside a transaction the entity is managed: Hibernate takes a snapshot when it loads the entity, compares at commit, and issues an update you never asked for.
EntityManager accessor instead. The following test changes a price without calling save (hibernate-data/src/test/java/com/ankurm/data/LibraryTest.java, method statelessMeansDetachedAndExplicit). Transcript output/ga/03-stateless-no-dirty-checking.txt:
changed price in memory to 1.00, no save() call
price re-read from database: 45.00
after explicit save(): 1.00
The price went back to 45.00 because nothing noticed the change; only the explicit save() wrote it. The Spring test (output/spring/03-managed-entities-dirty-check.txt) does the same thing in a transaction and gets the opposite result:
changed price to 1.00 in memory, no save() call, leaving the transaction
price re-read from database: 1.00
SQL Hibernate prepared:
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.isbn=?
sql> update book set author=?,isbn=?,price=?,published_year=?,title=? where id=?
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.published_year,b1_0.title from book b1_0 where b1_0.isbn=?
Here the update book set … was issued with no save() call. Neither behaviour is a bug. The stateless one is predictable and cheap for bulk work; the managed one is convenient and the classic source of “why did that row change”.
The idea summary for this article mentioned stateful repositories, so I tested it. The Hibernate 8.0 documentation says repositories are backed by a StatelessSession and that stateful repositories were not part of Jakarta Data 1.0. Still, declaring the accessor as an EntityManager makes hibernate-processor generate a repository backed by one. The interface is hibernate-data/src/main/java/com/ankurm/data/StatefulLibrary.java, and this is what happened on both lines (output/ga/07-stateful-repository.txt):
implementation of StatefulLibrary is generated as com.ankurm.data.StatefulLibrary_
changed price to 2.00 in memory, no save() call, committing
price re-read from database: 2.00
SQL Hibernate prepared:
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.isbn=?
sql> update Book set author=?,isbn=?,price=?,publishedYear=?,title=? where id=?
sql> select b1_0.id,b1_0.author,b1_0.isbn,b1_0.price,b1_0.publishedYear,b1_0.title from Book b1_0 where b1_0.isbn=?
Same repository shape, managed entity, an update with no save(). The pre-release line (output/h8/07-stateful-repository.txt) behaves identically. I want to be exact about what this proves: it proves Hibernate’s processor supports an EntityManager accessor today on both lines. It does not prove a portable, specified stateful model, because the specification’s own optional stateful API (jakarta.data:jakarta.data.stateful-api) has only milestones up to 1.1.0-M3 on Maven Central and I did not use it.
Going deeper: what stateless sessions cost you
With StatelessSession there is no first-level cache, no cascading and no lazy loading by proxy: the Hibernate documentation says lazy fetching is explicit through StatelessSession.fetch() and that operations never cascade. Never share one session across transactions. My earlier article on Hibernate 8’s stateless handler goes through those differences in detail.
- Hibernate 8 and Jakarta Persistence 4: EntityAgent and Stateless Sessions Explained
- The stateless test and the stateful test: hibernate-data/src/test/java/com/ankurm/data/LibraryTest.java
The two surprises that cost a compile cycle on Hibernate 8
Both of these only appeared because the code was run on the release candidate. One: the implementation class got renamed, covered earlier, in the smallest-repository section. Two:@Query methods are named queries that must be registered. On Hibernate 8.0.0.CR2, building a Configuration that registers only Book and then calling a @Query method fails at run time, and the repository interface has to be added too. The test calls cheaperThan with the repository left out (output/h8/06-query-registration.txt):
profile h8, repository NOT registered, calling cheaperThan(46.00):
DataException: No query named 'com.ankurm.data.Library#cheaperThan(java.math.BigDecimal)'
On 7.4.17 the same call works without registering anything (output/ga/06-query-registration.txt):
profile ga, repository NOT registered, calling cheaperThan(46.00):
worked without addAnnotatedClass(Library.class)
The fix is one line in hibernate-data/src/test/java/com/ankurm/data/Lab.java — cfg.addAnnotatedClass(Library.class) — and the test queryNeedsRegistration pins both behaviours so a later CR or the final release changes this section by failing a build.
The fingerprint to recognise:DataException: No query named 'com.ankurm.data.Library#cheaperThan(java.math.BigDecimal)'at run time, on a method that compiled. In a Spring or CDI container the repository type is normally discovered for you; this bites in plain Java SE tests and in hand-builtSessionFactorysetups. This is release-candidate behaviour; I have not seen it documented, and the final 8.0 release may change it.
Should you switch to Jakarta Data?
Not on the strength of this article alone, and not for the pre-release parts. If you are on Spring Boot with Spring Data JPA and your queries are mostly derived names plus a few@Querymethods, nothing here is a reason to rewrite. Spring Data is GA, familiar, integrates with transactions and Boot out of the box, and its typo checks run at context start, which a single context-loading test turns into a build failure.
Jakarta Data with Hibernate earns a look when: you want typo errors fromjavacrather than from a started context; you want stateless, explicit repositories for batch-style code; you are on Jakarta EE 11 or Quarkus, where the container injects the repository; or you want to avoid depending on Spring in the persistence layer. Do not build on@Select,@Isor@Firstfor production yet: they are in a milestone of a specification that is still “under development”, running on a release candidate. The GA line (7.4.17 with Jakarta Data 1.0.2) gives you@Find,@Query, the lifecycle annotations and compile-time checking today.
Production checklist
- Pin exact Hibernate and Jakarta Data versions together. The generated class naming changed between 7.4.17 and 8.0.0.CR2 (
Library_versus_Library), so a version bump can break code that names it. - Add the processor through
annotationProcessorPaths. As of JDK 23, javac no longer runs processors found on the plain class path by default; my first attempt with the processor only as aprovideddependency generated nothing, and the tests failed with “no generated implementation”. - Keep at least one test that loads every repository, whichever library you pick. For Spring that test is what turns a start-up failure into a build failure.
- Decide stateless or stateful per repository and write it down; the accessor type is the only visible signal.
- Never share a
StatelessSessionacross transactions (Hibernate documentation). - Treat
nullarguments to@Findas “is null” (see the generated code in the smallest-repository section) and validate at the boundary. - Do not ship
@Select,@Isor@Firstuntil Jakarta Data 1.1 is final and your Hibernate is a.Finalthat supports it. - Delete the compile-failure demo files (
src/broken) from anything you copy.
Reference: complete transcripts and the one-command rerun
Everything above is regenerated by ./scripts/run-all.sh (JDK 25 and Maven needed; first run downloads dependencies). The version check against Maven Central that backs the status callout at the top is output/00-version-check.txt and is produced by scripts/verify-versions.sh. This is its output from the run that generated the files in the repository:
org/hibernate/orm/hibernate-core
8.0.0.Beta3
8.0.0.Beta4
8.0.0.CR1
8.0.0.CR2
lastUpdated 20261124150926
org/hibernate/orm/hibernate-processor
8.0.0.Beta3
8.0.0.Beta4
8.0.0.CR1
8.0.0.CR2
lastUpdated 20261124150920
jakarta/data/jakarta.data-api
1.1.0-M1
1.1.0-M2
1.1.0-M3
1.1.0-M4
lastUpdated 20260827182903
jakarta/persistence/jakarta.persistence-api
4.0.0-M5
4.0.0-M6
4.0.0-M7
4.0.0-M8
lastUpdated 20261105172812
org/springframework/data/spring-data-jpa
4.2.0-M1
4.2.0-M2
4.2.0-RC1
4.2.0
lastUpdated 20261119150031
org/springframework/boot/spring-boot-starter-parent
4.2.0-M2
4.2.0-RC1
4.2.0-RC2
4.2.0
lastUpdated 20261119183517
newest Final (GA) hibernate-core:
7.4.17.Final
newest GA jakarta.data-api:
1.0.2
Other files not quoted above: output/ga/00-versions.txt, output/h8/00-versions.txt, output/h8/03-stateless-no-dirty-checking.txt, output/h8/01-find-by-parameter-name.txt, output/h8/02-query-and-record-projection.txt, output/spring/00-versions.txt.
Further reading
- Companion project: asmhatre/hibernate8-jpa4-demo/jakarta-data
- Hibernate Data Repositories (8.0 documentation): docs.hibernate.org/orm/8.0/repositories
- Hibernate ORM releases and compatibility matrix: hibernate.org/orm/releases
- Jakarta Data specification: jakarta.ee/specifications/data
- On ankurm.com: Hibernate 8 EntityAgent and stateless sessions, Hibernate 8 graph-based flush ordering, Spring Data JPA pagination
No Comments yet!