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
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
// Transaction 1: Check that no tweet matches the search string
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(em);
QueryBuilder tweetQb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Tweet.class).get();
Query fullTextQuery = tweetQb.keyword().onField(Tweet_.message.getName()).matching(“Message updated”).createQuery();
List results = fullTextEm.createFullTextQuery(fullTextQuery).getResultList();
Assert.assertEquals(0, results.size());
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
@Entity
public class Book implements Serializable {
@Column
@NotNull
@Size(min=5, max=20)
private String title;
@Entity
public class Author implements Serializable {
@Enumerated(EnumType.ORDINAL)
private AuthorStatus status;
}
TypedQuery q = em.createQuery(“SELECT a FROM Author a WHERE a.id = :id”, Author.class);
q.setParameter(“id”, 1L);
q.setHint(“org.hibernate.comment”, “This is my comment”);
Author a = q.getSingleResult();
SELECT a FROM Author a WHERE a.firstName like ‘%and%’ and a.id >= 20 and size(author.books) >= 5
@Entity
public class Author {
@ManyToMany(mappedBy=”authors”, cascade = CascadeType.PERSIST)
private List<Book> books = new ArrayList<Book>();
}
AuditQuery q = auditReader.createQuery().forRevisionsOfEntity(Book.class, false, true);
q.addProjection(AuditEntity.revisionNumber());
q.add(AuditEntity.revisionProperty(“userName”).eq(“User 1”));
List<Number> revisionNumbers = q.getResultList();
@Entity
public class Author {
@PrePersist
private void initializeCreatedAt() {
this.createdAt = LocalDateTime.now();
log.info(“Set createdAt to “+this.createdAt);
}