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
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@OneToMany(mappedBy = “book”)
private List reviews = new ArrayList();
Query q = em.createQuery("SELECT a, size(a.books) FROM Author a GROUP BY a.id");
List<Object[]> results = q.getResultList();
Statistics stats = sessionFactory.getStatistics();
long queryCount = stats.getQueryExecutionCount();
long collectionFetchCount = stats.getCollectionFetchCount();
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure()
.build();
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
private LocalDate publishingDate;
Book b = em.find(Book.class, 1L);
Author a = new Author();
a.setFirstName("Thorben");
a.setLastName("Janssen");
a.getBooks().add(b);
b.getAuthors().add(a);
em.persist(a);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(em);
QueryBuilder tweetQb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Tweet.class).get();
Query tweetQuery = tweetQb.all().createQuery();
FullTextQuery fullTextQuery = fullTextEm.createFullTextQuery(tweetQuery, Tweet.class);
@Entity
public class Author {
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
...
}
@AnalyzerDef(
name = “textanalyzer”,
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class,
params = { @Parameter(name = “language”, value = “English”) })
}
)
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Book.class);
Root root = cq.from(Book.class);
// call the database function calculate
ParameterExpression doubleParam1 = cb.parameter(Double.class);
ParameterExpression doubleParam2 = cb.parameter(Double.class);
cq.where(cb.greaterThan(doubleParam2, cb.function(“calculate”, Double.class, root.get(Book_.price), doubleParam1)));
TypedQuery q = em.createQuery(cq);