Cheat Sheet: New Features in JPA 2.1

JPA 2.1 introduced 12 new features, like StoreProcedureQueries, Entity Graphs and Attribute Converter, to make your work with the database easier and more efficient.
Download your free New Features in JPA 2.1 cheat sheet now, to get all the information you need to improve your database access.

This 5 page cheat sheet brings you:

- a short description and
- code snippets for each feature,
- links to more detailed articles.

Signup now and get your free New Features in JPA 2.1 cheat sheet and regular blog updates.

I respect your privacy and have ZERO TOLERANCE for spam!

JPA 2.1 Entity Graph - Part 1: Named entity graphs

Lazy loading was often an issue with JPA 2.0. You have to define at the entity if you want to use FetchType.LAZY (default) or FetchType.EAGER to load the relation and this mode is always used. FetchType.EAGER is only used if we want to always load the relation. FetchType.LAZY is used in almost all of the cases to get a well performing and scalable application.
But this is not without drawbacks. If you have to use an element of the relation, you need to make sure, that the relation gets initialized within the transaction that load the entity from the database. This can be done by using a specific query that reads the entity and the required relations from the database. But this will result in use case specific queries. Another option is to access the relation within your business code which will result in an additional query for each relation. Both approaches are far from perfect.

JPA 2.1 entity graphs are a better solution for it. The definition of an entity graph is independent of the query and defines which attributes to fetch from the database. An entity graph can be used as a fetch or a load graph. If a fetch graph is used, only the attributes specified by the entity graph will be treated as FetchType.EAGER. All other attributes will be lazy. If a load graph is used, all attributes that are not specified by the entity graph will keep their default fetch type.

Lets have a look how to define and use an entity graph.

SSL encrypted EJB calls with JBoss AS 7


Encrypting the communication between client and server provides improved security and privacy protection for your system. This can be an important requirement by the customer, especially if client or server need to work in an unprotected network.

This article shows you how to setup SSL encrypted EJB calls in JBoss AS 7.

Generate your JAXB classes in a second with xjc

Since JAXB is part of the JDK, it is one of the most often used frameworks to process XML documents. It provides a comfortable way to retrieve and store data from XML documents to Java classes. As nearly every Java developer has already used JAXB, I will not explain the different JAXB annotations. Instead I will focus on a little command line tool called xjc and show you how to generate your binding classes based on an existing XSD schema description.

JPA 2.1 Attribute Converter - The better way to persist enums

Persisting enums with JPA 2.0 is possible, but there is no nice way to do it. Using the @Enumerated annotation, you can use EnumType.ORDINAL or EnumType.STRING to map the enum value to its database representation. But both options have their drawbacks. The ordinal of an Enum depends on the ordering of its values and can create problems, if we need to add new ones. The String representation of an Enum is often quite verbose and renaming a value will break the database mapping. These drawbacks can be avoided by using an Attribute Converter.


Criteria Update/Delete - The easy way to implement bulk operations with JPA2.1

With JPA2.1 the Criteria API was extended by CriteriaUpdate and CriteriaDelete. The two classes can be used to implement bulk update and delete operations using the Criteria API.

Links of the Week (CW41) - Wildfly

Hello and welcome to "Links of the week (CW41)" :-)

This week was really busy for me, so there was not so much time to read interesting articles. But that does not mean, that nothing happened. Wildfly 8.0.0.Beta1 was released on the 4th and it became my main topic of the last days. It is the first Wildfly release which implements all user facing EE7 APIs and contains several other improvements like the new high performance web server Undertow. Check the Release Notes and Documentation for more information.

Bear Giles wrote a great article about Database Encryption Using JPA Listeners. He describes a nice and easy way to separate persistence and security by using JPA Listeners.
JPA 2.1 Type Converter are an alternative way to encrypt data before persisting it in the database. A Type Converter can be used to convert an attribute from its entity representation to its database representation and back. If you want to learn more about Type Converters, have a look at the article I wrote on Monday: JPA 2.1 - How to implement a Type Converter


Have fun!
Thorben

JPA 2.1 - How to implement an Attribute Converter

JPA 2.1 brings several improvements. One of them is the Attribute Converter. It allows the developer to specify methods to convert between the database and the Java representation of an attribute.