Add jakarta-data module: Jakarta Data repositories on Hibernate 7.4 / 8.0 CR2 vs Spring Data JPA 4.2
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4XrpfdpsacrvqnrBM
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
profile ga
|
||||
hibernate 7.4.17.Final
|
||||
jakarta.data 1.0.2
|
||||
java 25.0.5.1+2-LTS
|
||||
implementation of Library is generated as com.ankurm.data.Library_
|
||||
@@ -0,0 +1,7 @@
|
||||
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=?
|
||||
@@ -0,0 +1,7 @@
|
||||
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=?
|
||||
@@ -0,0 +1,3 @@
|
||||
changed price in memory to 1.00, no save() call
|
||||
price re-read from database: 45.00
|
||||
after explicit save(): 1.00
|
||||
@@ -0,0 +1,8 @@
|
||||
##### BadHql
|
||||
com/ankurm/data/BadHql.java:[15,12] Could not interpret path expression 'prise'
|
||||
##### 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'
|
||||
@@ -0,0 +1,2 @@
|
||||
profile ga, repository NOT registered, calling cheaperThan(46.00):
|
||||
worked without addAnnotatedClass(Library.class)
|
||||
@@ -0,0 +1,7 @@
|
||||
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=?
|
||||
@@ -0,0 +1,238 @@
|
||||
package com.ankurm.data;
|
||||
|
||||
import jakarta.data.exceptions.DataException;
|
||||
import jakarta.data.exceptions.EmptyResultException;
|
||||
import jakarta.data.exceptions.EntityExistsException;
|
||||
import jakarta.data.exceptions.OptimisticLockingFailureException;
|
||||
import jakarta.persistence.NoResultException;
|
||||
import jakarta.persistence.NonUniqueResultException;
|
||||
import jakarta.persistence.PersistenceException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import java.util.Optional;
|
||||
import org.hibernate.StaleStateException;
|
||||
import org.hibernate.StatelessSession;
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import static org.hibernate.query.Order.asc;
|
||||
import static org.hibernate.query.SortDirection.*;
|
||||
import org.hibernate.query.specification.SelectionSpecification;
|
||||
|
||||
/**
|
||||
* Implements Jakarta Data repository {@link com.ankurm.data.Library}
|
||||
**/
|
||||
public class Library_ implements Library {
|
||||
|
||||
|
||||
/**
|
||||
* @see #cheaperThan(BigDecimal)
|
||||
**/
|
||||
static final String CHEAPER_THAN_BigDecimal = "from Book where price < :max order by price";
|
||||
|
||||
/**
|
||||
* @see #titlesBy(String)
|
||||
**/
|
||||
static final String TITLES_BY_String = "select title, publishedYear from Book where author = :author order by publishedYear";
|
||||
|
||||
/**
|
||||
* @see #countBy(String)
|
||||
**/
|
||||
static final String COUNT_BY_String = "select count(*) from Book where author = :author";
|
||||
|
||||
|
||||
protected StatelessSession session;
|
||||
|
||||
public Library_(StatelessSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatelessSession session() {
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Book book) {
|
||||
requireNonNull(book, "Null book");
|
||||
try {
|
||||
session.insert(book);
|
||||
}
|
||||
catch (ConstraintViolationException _ex) {
|
||||
throw new EntityExistsException(_ex.getMessage(), _ex);
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Book book) {
|
||||
requireNonNull(book, "Null book");
|
||||
try {
|
||||
if (session.getIdentifier(book) == null)
|
||||
session.insert(book);
|
||||
else
|
||||
session.upsert(book);
|
||||
}
|
||||
catch (StaleStateException _ex) {
|
||||
throw new OptimisticLockingFailureException(_ex.getMessage(), _ex);
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Book book) {
|
||||
requireNonNull(book, "Null book");
|
||||
try {
|
||||
session.delete(book);
|
||||
}
|
||||
catch (StaleStateException _ex) {
|
||||
throw new OptimisticLockingFailureException(_ex.getMessage(), _ex);
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find {@link Book} by {@link Book#isbn isbn}.
|
||||
*
|
||||
* @see com.ankurm.data.Library#byIsbn(String)
|
||||
**/
|
||||
@Override
|
||||
public Optional<Book> byIsbn(String isbn) {
|
||||
var _builder = session.getCriteriaBuilder();
|
||||
var _query = _builder.createQuery(Book.class);
|
||||
var _entity = _query.from(Book.class);
|
||||
_query.where(
|
||||
isbn==null
|
||||
? _entity.get(Book_.isbn).isNull()
|
||||
: _builder.equal(_entity.get(Book_.isbn), isbn)
|
||||
);
|
||||
try {
|
||||
var _select = session.createSelectionQuery(_query);
|
||||
return _select
|
||||
.uniqueResultOptional();
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find {@link Book} by {@link Book#author author}.
|
||||
*
|
||||
* @see com.ankurm.data.Library#byAuthor(String)
|
||||
**/
|
||||
@Override
|
||||
public List<Book> byAuthor(String author) {
|
||||
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)
|
||||
);
|
||||
var _spec = SelectionSpecification.create(_query);
|
||||
_spec.sort(asc(Book.class, "publishedYear"));
|
||||
try {
|
||||
var _select = _spec.createQuery(session);
|
||||
return _select
|
||||
.getResultList();
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find {@link Book} by {@link Book#author author} and {@link Book#publishedYear publishedYear}.
|
||||
*
|
||||
* @see com.ankurm.data.Library#byAuthorAndPublishedYear(String,int)
|
||||
**/
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query {@value #CHEAPER_THAN_BigDecimal}.
|
||||
*
|
||||
* @see com.ankurm.data.Library#cheaperThan(BigDecimal)
|
||||
**/
|
||||
@Override
|
||||
public List<Book> cheaperThan(BigDecimal max) {
|
||||
try {
|
||||
var _select = session.createSelectionQuery(CHEAPER_THAN_BigDecimal, Book.class)
|
||||
.setParameter("max", max);
|
||||
return _select
|
||||
.getResultList();
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query {@value #TITLES_BY_String}.
|
||||
*
|
||||
* @see com.ankurm.data.Library#titlesBy(String)
|
||||
**/
|
||||
@Override
|
||||
public List<TitleAndYear> titlesBy(String author) {
|
||||
try {
|
||||
var _select = session.createSelectionQuery(TITLES_BY_String, TitleAndYear.class)
|
||||
.setParameter("author", author);
|
||||
return _select
|
||||
.getResultList();
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query {@value #COUNT_BY_String}.
|
||||
*
|
||||
* @see com.ankurm.data.Library#countBy(String)
|
||||
**/
|
||||
@Override
|
||||
public long countBy(String author) {
|
||||
try {
|
||||
var _select = session.createSelectionQuery(COUNT_BY_String, long.class)
|
||||
.setParameter("author", author);
|
||||
return _select
|
||||
.getSingleResult();
|
||||
}
|
||||
catch (NoResultException _ex) {
|
||||
throw new EmptyResultException(_ex.getMessage(), _ex);
|
||||
}
|
||||
catch (NonUniqueResultException _ex) {
|
||||
throw new jakarta.data.exceptions.NonUniqueResultException(_ex.getMessage(), _ex);
|
||||
}
|
||||
catch (PersistenceException _ex) {
|
||||
throw new DataException(_ex.getMessage(), _ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user