Java Persistence API: Controlling the Second-Level-Cache

Using the Java Persistence API and a decent persistence provider allows us to configure and fine-tune when and how the second level cache is used in our application. In the following short examples, we’re going to demonstrate those features written as JUnit test cases and running on a H2 in-memory database. Figure 1. Persistence Unit Configuration Setup First of all we need some basic setup to run the following examples .. we need to select a JPA persistence provider and database, create a persistence-unit configuration and an environment to run tests on an in-memory database. ...

April 21, 2014 · 7 min · 1417 words · Micha Kops

Creating elegant, typesafe Queries for JPA, mongoDB Morphia and Lucene using Querydsl

Querydsl is a framework that allows us to create elegant, type-safe queries for a variety of different data-sources like Java Persistence API (JPA) entities, Java Data Objects (JDO), mongoDB with Morphia, SQL, Hibernate Search up to Lucene. In the following tutorial we’re implementing example queries for different environments – Java Persistence API compared with a JPQL and a criteria API query, mongoDB with Morphia and last but not least for Lucene. ...

February 13, 2014 · 9 min · 1879 words · Micha Kops

Easy Database Migrations using Flyway, Java EE 6 and GlassFish

Database migrations often are a necessity in the application development and maintenance life-cycle. Whenever we need to apply changes to the database structure, insert new data fragments and in doing so want to be sure that this all happens with some control and versioning. The following tutorial shows how implement this for a simple Java EE 6 web application to be run on a GlassFish application server in a few quick steps using the Flyway framework, an eager initialized Singleton EJB and some Maven wiring. ...

April 28, 2013 · 9 min · 1742 words · Micha Kops

Adding multiple EntityListeners to an Entity in JPA 2

The ability to attach lifecycle events to an entity using simple annotations sometimes is a neat feature in the Java Persistence API. The following short snippets demonstrate how to bind and trigger the different available lifecycle events using an embedded derby database and a bunch of annotations. Dependencies I’m using Hibernate as persistence manager here and Derby as an easy to setup database. In the last step we’ll be writing a test that’s why we’ve added JUnit and Hamcrest. ...

February 25, 2013 · 6 min · 1098 words · Micha Kops

Hibernate Search Faceting: Discrete and Range Faceting by Example

In today’s tutorial we’re exploring the world of faceted searches like the one we’re used to see when we’re searching for an item on Amazon.com or other websites. We’re using Hibernate Search here that offers an API to perform discrete as well as range faceted searches on our persisted data. Maven Dependencies Needed For simplicity’s sake am I going to use an HSQL database for persistence, in addition the dependencies for hibernate-entitymanager and hibernate-search (of course) should be added to your pom.xml ...

March 26, 2012 · 5 min · 986 words · Micha Kops

JPA Persistence and Lucene Indexing combined in Hibernate Search

Often we’re writing an application that has to handle entities that – on the one side need to be persisted in a relational database using standards like the Java Persistence API (JPA) and using frameworks like Hibernate ORM or EclipseLink. On the other side those entities and their fields are often stored in a highspeed indexer like Lucene. From this situation arises a bunch of common problems .. to synchronize both data sources, to handle special data mapped in an entity like an office document and so on.. ...

February 5, 2012 · 6 min · 1170 words · Micha Kops

Creating a sample Java EE 6 Blog Application with JPA, EJB, CDI, JSF and Primefaces on GlassFish

Java EE 6 is out and it indeed offers an interesting stack of technologies. So in today’s tutorial we are going to build a small sample web application that builds on this stack using Enterprise JavaBeans, Java Persistence API, Bean Validation, CDI and finally Java Server Faces and PrimeFaces. The application we’re going to develop is a simple blog app that allows us to create new articles, list them and – finally delete them. We’re also covering some additional topics like JSF navigation, i18n, Ajax-enabled components and the deployment on the GlassFish application server. ...

February 8, 2011 · 17 min · 3575 words · Micha Kops

Object-relational Mapping using Java Persistence API / JPA 2

Today we’re going to take a look at the world of object-relational Mapping and how it is done using the Java Persistence API by creating some basic examples, mapping some relations and querying objects using JPQL or the Criteria API.. Prerequisites Java 6 JDK Maven >= 2 If you’d like to take a look behind the scenes e.g. how entities are mapped in your database you could install a RDBMS of your choice .. or just use Derby/JavaDB that is bundled with the JDK 6 ...

October 11, 2010 · 13 min · 2668 words · Micha Kops

Java Server Faces/JSF 2 Tutorial – Step 1: Project setup, Maven and the first Facelet

In this short tutorial we are going to build a Java Server Faces Web-Application using JSF2.0, Facelets, Maven and Hibernate as ORM Mapper. The goals for this first step are: Setting up the project structure using Maven, defining a frame template/decorator and a registration facelet, creating a managed bean and mapping it’s values to the facelet, adding some basic validation, displaying validation errors and finally adding a navigation structure. In step2 of this tutorial we are going to add persistence using Hibernate, add some security, create a custom UI component and add some AJAX. ...

June 5, 2010 · 9 min · 1841 words · Micha Kops

Named Queries in Grails 1.2

They built a nice new feature in Grails 1.2 called “named queries“. Named queries can be defined in a domain class as static properties and support the criteria builder syntax. Examples package testapp class User { String name int iq int age static namedQueries = { dumbUsers { int referenceIq = 60 lt 'iq' , referenceIq } nameStartsWith { letter -> like 'name', '${letter}%' } midAges { between('age', 20, 40) } } } // count dumb users println User.dumbUsers.count() // print amount of users, usernames starting with an 'a' User.nameStartsWith('a').count() ...

April 6, 2010 · 1 min · 111 words · Micha Kops