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!

Java Weekly 50: Nashorn improvements, Jigsaw, CDI 2.0 sneak peak and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



5 ways to initialize lazy relations and when to use them

Lazy loading of relations between entities is a well established best practice in JPA. Its main goal is to retrieve only the requested entities from the database and load the related entities only if needed. That is a great approach, if we only need the requested entities. But it creates additional work and can be the cause of performance problems, if we also need some of the related entities.

Lets have a look at the different ways to trigger the initialization and their specific advantages and disadvantages.




Java Weekly 49: Java doesn't suck, annotations everywhere, free ebooks and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Review "Java Performance: The Definitive Guide" by Scott Oaks

Java Performance: The Definitive Guide
I recently finished the book Java Performance: The Definitive Guide* by Scott Oaks and I really liked it!

The book is very well written and Scott gives a lot of in-depth explanations about complex topics like JIT compilation, garbage collector algorithms and thread synchronization. I learned a lot about different ways to analyze the performance of an application and how to optimize it. And I am sure that this book will be very useful when I have to analyze the next performance issue.
I recommend it to every experienced Java developer who likes to learn more about performance optimization.

But lets get into more details ...

Java Magazin published my first article

Hey,

I just wanted to tell all german readers, that the Java Magazin published my first article in the current issue 1.15. It's called "Aus A mach B - Konvertierung mithilfe von JPA Attribute Converter" and describes (guess what) the usage of the new JPA 2.1 Attribute Converter.



You don't speak german and like to learn more about Attribute Converter? No problem, I also wrote several blog posts about it:







Java Weekly 48: Modern APIs, Entity Graph, agile specs and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Java Weekly 47: Java 9, tweet index, compress and authenticate REST service and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Java Weekly 46: Joda-Time to Java8, new Apache Tamaya, Java internals and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.


No Java Weekly this week

Hey everyone,

I went to the GeeCon conference last week and had no time to read articles for the Java Weekly. So I have to skip it for this week.

Bye,
Thorben

Java Weekly #16: Named Parameters, Java Batch, JavaOne Recordings and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.


Java Weekly #15: JavaOne, Lambdas, authentication, HTTP/2 and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.


Java Weekly #14: Java EE 8, JavaOne, story writing and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Java Weekly #13: Everything Java, real Java EE, new config JSR and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Java Weekly #12: JavaEE Boot, Java 9, functional programming and more ...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.


Java Weekly #11: Missing stream method, Java 9 overview, lost updates and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.


Java Weekly #10: Concurrency, no config JSR, MVC, JCP and more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Java Weekly #9: Money, retired DTOs, JSR for MVC, JDK tools aand more...

The Java world is evolving at a rapid pace and it can be challenging to keep track of it. Fortunately lots of great resources are created every week, explaining new features or looking at existing stuff from a different angle.

I am using the Java Weekly series to collect the most interesting links I found during the last week and present them to you all in one place. I hope you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment.



Java Weekly #8: Java9, JMS 2, JUnit, Microservices and more...

The Java ecosystem is evolving at a rapid pace and it can be a challenge to keep track on it. Fortunately lots of resources are created every week, explaining new features or looking at existing stuff from a different angle.
I am using the Java Weekly series to collect the most interesting resources I found during the week and present them to you all in one place. I hope that you find it useful and that it makes it easier for you to keep up-to-date. If you like to suggest a resource or something I can improve on, please leave me a comment. I will definitely read it and come back to you.


Java 9

Mark Reinhold announced the first batch of JEPs for Java 9. These are:

For me as a developer the two most interesting JEPs are 198 Light-Weight JSON API and 201 Modular Source Code. JEP 201 is only the first step for the JIGSAW project and JEP 200 will be more interesting. But it is good to see, that it is targeted for Java 9.


Java EE

The Java Temporary Cache API (JCache) was released a few month ago and was one of the most wanted features in the Java EE 8 community surveys. If you like to refresh your knowledge of this specification, you should have a look at Java Temporary Caching API aka JCache by David Delabassee. He gives a short overview of existing resources and upcoming JavaOne sessions.

Java Weekly #7: Generics with Lambda, unified type conversion and more...

Java

Java generics are an important concept in the modern Java development and they are key to understand and use the new Java 8 lambda expressions to its full extend. Josh Juneau wrote a great article, explaining how to use Java generics and why we need them to use lambda expressions.


Javin Paul wrote a great article about a small change introduced with JDK 1.7.0_40. Empty ArrayList and HashMap are now initialized with a capacity of 0, instead of an Object array of size 10 for an empty ArrayList and a default capacity of 16 for an empty HashMap. Depending on the application, this can be a huge performance improvement.


The Java update 8u11 and 7u65 introduced some errors and broke several third-party tools. This was the result of a stricter verification by the bytecode verify as described by Niv Steingarten in his post Oracle’s Latest Java 8 Update Broke Your Tools — How Did it Happen?


Java EE

Arjan Tijms had a look at the different approaches on how to convert objects in Java EE. He describes 5 (yes, five!) ways which are currently defined in different Java EE specifications and names two more APIs which are part of Java SE. 
This raises the questions, if we need to define one unified conversion API. From my point of view, this should have been already done in the past. But better now then never...

Java Weekly #6: Micro Services, CDI 2.0, NoEstimates and more...

Java

After using Scala for some days and switching back to Java, Lucas Eder started to miss some features of the Scala syntax. He wrote a blog post about The 10 Most Annoying Things Coming Back to Java After Some Days of Scala

Java EE

Roberto Cortez wrote a great tutorial on how to deploy Spring Batch as a Wildfly module. You should have a look, if you want to use one of the most feature rich implementations of JSR-352 with Wildfly.


Monitoring the O/R mapper becomes an important task, if you need to analyze performance issues of a Java EE application. Therefore Hibernate provides its Statistics Interface which exposes several statistics related to sessions, entites, queries and more. If you like to learn how to activate these statistics and how you can access them, have a look at Hibernate Statistics with Hawtio and Jolokia by Markus Eisele.

Java Weekly #5: Metaspace, Server-Sent Events, Java EE 8 drafts and more...

Java

If you like to learn more about the removal of PermGen and the introduction of Metaspace in Java 8, you should have a look at Where Has the Java PermGen Gone? by Monica Beckwith. She wrote a great post about problems with the old PermGen and how they were solved with the new Metaspace.


Java EE

Server-Sent Events (SSE) were introduced with HTML 5 and enable the server to push data via HTTP to the client. Shing Wai Chan shows in his post Server-Sent Events with Async Servlet By Example how you can implement SSE by only using the Servlet API.


Antonio Goncalves wrote an interesting article about integration testing. He wants to execute the integration tests of his web application only, if it is successfully deployed on the JBoss application server. Otherwise the tests shall be skipped. He describes in his post Your tests assume that JBoss is up and running how this can be achieved by using JUnit assumptions and the JBoss HTTP management API.

Java Weekly #4: PicketLink and DeltaSpike, Batch API, JMS 2.1 and more...

Java EE

Shane Bryzak wrote a great article on how to use PicketLink and Apache DeltaSpike to secure your Java EE application. The combination of these two frameworks is really powerful. It allows you to add security to your application by implementing only one annotation and one method.


The Batch 1.0 API does not offer any solution to schedule a batch operation in Java EE. But there is no need for it, as Arun Gupta shows in his Tech Tip #36. The Java EE plattform already offers 3 different ways to schedule a batch operation.


If you want to learn more about the Java EE Concurrency API, you should have a look at the Java EE Concurrency API Tutorial by Francesco Marchioni. He created a detailed description on how to process parallel tasks on a Java EE application server. This is really a great resource if you want to learn more about Java EE Concurrency.

Java Weekly #3: Microservices, Java 8 features, upcoming events and more...

Java

The article Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional! by Raoul-Gabriel Urma gives an extensive description about how to use Optional to improve your API and avoid NullPointerExceptions.


Oleg Shelajev wrote an interesting article about the unpredictability of parallel streams. The stream API is one of the major features introduced with Java 8 and it seems to be easy and powerful. But as Oleg describes in his article, it can influence the performance of your application in an unpredictable way.


Java EE

Maven archetypes are a quick and easy way to setup a Maven project and immediately start with the implementation. But so far there was no archetype to create a Java EE 7 project with Arquillian dependencies and profiles (at least as far as I know...). If you have used Arquillian to test your application, you know that setting up the profiles for different application servers is always some annoying (copy & paste) work.
This has changed since Arun Gupta introduced a Maven Archetype for Java EE 7 projects with Arquillian profiles on his blog. The archetype provides Java EE 7 dependencies and creates a managed and a remote profile for Wildfly and GlassFish.

Java Weekly #2: JPA 2.1, Java8, JSR 351, Eclipse Luna and more...

Java EE

Steven Gertiser showed a great example on how to use a JPA Type Converter to persist the new Java8 DateTime classes. This example shows again how powerful the type converter feature is. You only need a few lines of code to define how a class gets persisted to the database.
If you like to read more about Type Converter and other new features of JPA 2.1, have a look at my JPA 2.1 related posts.


Normally, I try to ignore the Spring vs. Java EE discussion, but I found a good posting by Nmpallas this week. He is a long time Java EE developer and gave Spring a try for a recent project. His main reason was, that an application server offers much more features than he needed. But after some time, he decided to migrate the application to Wildfly 8.0.0. Read about his reasons and what he thinks about the standard Spring arguments in: It’s not Spring anymore is the summer of JEE7, ready riding the wave?


Java 8

Java 8 introduced the two default methods forEach(Consumer action) and spliterator() to the Iterable interface. While this looks good in the first place, Stephen Colebourne is not as happy with it. He describes in his blog why this change reduced the number of use cases of the Iterable interface: Java 8 - Iterable woes
I do not see a huge issue in this change. But what do you think? Is this really a drawback of the new Iterable interface?

Java EE Pitfalls #1: Ignore the default lock of a @Singleton

EJB Singleton Beans were introduced by the EJB 3.1 specification and are often used to store cached data. This means, we try to improve the performance of our application by using a Singleton. In general, this works quite well. Especially if there are not too many calls in parallel. But it changes if we ignore the default lock and the number of parallel calls increases.

Java Weekly #1: CDI, Java8, Bean Validation and more...

CDI

Are you familiar with CDI events? Really?
Well, then you might skip the beginning of the article You think you know everything about CDI events… Think again! by Antoine Sabot-Durand. He gives a great overview about how CDI events work and their (current) drawbacks. And he gives a small sneak peak at the changes we can expect with CDI 2.0.
This is by far the best overview about CDI events I have read so far. Everyone who is using them, should have a look at it.


The Apache DeltaSpike team released version 1.0 this week. You can find the release notes here and a short overview about the different modules in this posting by Rafael Benevides.
DeltaSpike looks really interesting and seems to provide several features I have missed or found quite complicated to implement. I have never used it so far, but I am planning to change that. If anyone of you has some real world experience with it, please write a comment. I love to hear about it.


Java 8

Oleg Shelajev takes a critical look at the usage of default methods introduced with Java 8. His main point is, that the usage of default methods can make it really difficult to understand your code. If you combine default methods of different interfaces with inheritance, it can become really difficult to find which method implementation will be executed. Therefore, you should think carefully before using default methods everywhere. Quiet often you can use inheritance to get to the same result.
I really liked his post. It is tempting to use new features as often as possible. But we always need to be aware of possible drawbacks. And a loss of readability and an increased complexity of our code can be a massive drawback if you or one of your colleagues has to change or analyze the code.

Testing with Aliens: How to test a JPA type converter with Arquillian

This post was written together with +Aslak Knutsen (@aslakknutsen).

JPA type converters provide an easy way to define how an entity attribute gets persisted to the database. You can use them to implement lots of different features, e.g. to encrypt your data as I showed in a previous post: How to use a JPA Type Converter to encrypt your data
But writing the type converter is not enough. We also need to make sure, that it is working correctly.

In general, there are two ways to test a type converter. We could write a unit test to check, if the conversion works correctly. But a unit test performs a test of the isolated class without putting it into the real execution environment. That means that we will still not know, if the converter works in one of our applications. If everything is set up correctly, the persistence provider will call the converter before writing to and after reading from the database. So we also need to check if the type converter gets called by the persistence provider and if everything works fine under that condition. We need to test the converter inside of the container we want to use for our application.
We will have a look at how this can be done with Arquillian and its persistence extension.

How to use a JPA Attribute Converter to encrypt your data

A few days ago, I read an interesting article by Bear Giles about Database encryption using JPA listeners from 2012. He discusses his requirements for an encryption solution and provides a code example with JPA listeners. His main requirements are:
  • provide a transparent encryption that does not affect the application,
  • be able to add the encryption at deployment time,
  • develop application and security/encryption by two different teams/persons.
And I completely agree with him. But after 1.5 years and a spec update to JPA 2.1, JPA listeners are not the only solution anymore. JPA 2.1 introduced Attribute Converter, which can be used to create a maybe better solution.

JPA 2.1 Entity Graph - Part 2: Define lazy/eager loading at runtime

This is my second post on Entity Graphs, one of the features introduced with JPA 2.1. The first post described the usage of named entity graphs. These can be used to define a graph of entities and/or attributes at compile time that shall be fetched with a find or query method. Dynamic entity graphs do to the same but in a dynamic way. This means you can use the EntityGraph API to define your entity graph at runtime.
If you have missed the first post and want to read how to define a named entity graph or how lazy loading issues were solved with JPA 2.0, check this post: JPA 2.1 Entity Graph - Part 1: Named entity graphs

Conference Report: JUG Saxony Day 2014

Last friday (2014-04-04) I visited the JUG Saxony Day 2014 in Dresden, Germany. It was the first event organized by the JUG Saxony and to make it short, it was great! The event was well organized and had lots of interesting talks.
But let's get into more details ...

The conference offered 4 tracks with 5 sessions each:

  1. Software development process
  2. Java technology
  3. Research
  4. Java mobile and enterprise applications with Java

So I had to make a hard decision to select 5 tracks for the day...

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.