Skip to content

Instantly share code, notes, and snippets.

View thjanssen's full-sized avatar
🎓
Creating the best Java persistence courses for the @Persistence-Hub

Thorben Janssen thjanssen

🎓
Creating the best Java persistence courses for the @Persistence-Hub
View GitHub Profile
AuditQuery q = auditReader.createQuery().forEntitiesAtRevision(Book.class, 2);
q.traverseRelation(“publisher”, JoinType.LEFT, “p”);
q.add(AuditEntity.and(
AuditEntity.or(AuditEntity.property(“title”).ilike(“JPA”, MatchMode.ANYWHERE), AuditEntity.property(“title”).ilike(“Hibernate”, MatchMode.ANYWHERE)),
AuditEntity.property(“p”, “name”).ilike(“Manning”)));
q.addOrder(AuditEntity.property(“title”).asc());
List<Book> audit = q.getResultList();
@Entity
public class Book implements Serializable {
@ManyToOne
@JoinColumn(name=”publisherid”)
private Publisher publisher;
@Entity
@Audited
public class Author implements Serializable { … }
10:51:52,378 DEBUG SQL:92 -
select
book_aud0_.REV as col_0_0_
from
Book_AUD book_aud0_ cross
join
REVINFO defaultrev1_
where
book_aud0_.id=?
and book_aud0_.REV=defaultrev1_.REV
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery q = cb.createQuery(AuthorValue.class);
Root root = q.from(Author.class);
q.select(cb.construct(AuthorValue.class, root.get(Author_.firstName), root.get(Author_.lastName)));
List authors = em.createQuery(q).getResultList();
@Entity
@NamedQuery(name = “Account.FindByName”, query = “SELECT a FROM Account a WHERE name like :name”)
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@Column
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
@SqlResultSetMapping(name = "BookValueMapping",
classes = @ConstructorResult(
targetClass = BookValue.class,
columns = {@ColumnResult(name = "title"),
@ColumnResult(name = "date")}
)
)
@Entity
@Table(name = "author", schema = "bookstore")
public class Author { … }
10:33:49,612 DEBUG [org.hibernate.SQL] – insert into Author (firstName, lastName, version, id) values (?, ?, ?, ?)
10:33:49,620 DEBUG [org.hibernate.SQL] – select author_.lastUpdate as lastUpda4_0_ from Author author_ where author_.id=?
10:33:49,644 DEBUG [org.hibernate.SQL] – update Author set firstName=?, lastName=?, version=? where id=? and version=?
10:33:49,646 DEBUG [org.hibernate.SQL] – select author_.lastUpdate as lastUpda4_0_ from Author author_ where author_.id=?