tag:blogger.com,1999:blog-53551062627637669452024-02-21T14:01:37.249+01:00Thoughts on JavaAnonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.comBlogger64125tag:blogger.com,1999:blog-5355106262763766945.post-26426051363170454902015-04-21T05:13:00.000+02:002015-04-21T05:13:14.841+02:00Result Set Mapping: Constructor Result MappingsThis is the third part of my series about SQL result set mappings:<br />
<ul>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">Result Set Mapping: The Basics</a></li>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">Result Set Mapping: Complex Mappings</a></li>
<li><b>Result Set Mapping: Constructor Result Mappings</b></li>
<li>Result Set Mapping: Hibernate specific features <i>(coming soon)</i></li>
</ul>
<div>
In the first post of this series, we had a look at some mapping definition between the query result and one entity. The mapping definitions got more complex in the second part, as we mapped the query result to multiple entities and handled additional columns. </div>
<div>
In this post, we will have a look at the Constructor Result Mappings <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">introduced in JPA 2.1</a>. This feature allows us to call the constructor of a value object with the result of the query, similar to the JPQL constructor expressions. This is often used, if we want to provide a specific view of our domain model to the client.<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOJj6ywjJQyO3Vl89LsREivKhh5igSENbuk2fZCnRKgfHqNjRfoLZqGxCNBE47RGQEbPJWcPKw4UPGSx6Zjxd3t0XxzZkLnvXSTA33vcgwPho1VOu4F6paMH1aEkOZl3MNLraa2ZaLDSS0/s1600/Result+Set+Mappings+-+Constructor.png" width="100%" />
</div>
<div>
<a name='more'></a><h2>
The example</h2>
Before we start, lets have a look at the entity model that we will use for the examples. If you read the <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">second part of this series</a>, you are already familiar with the <i>Author</i> and <i>Book</i> entities. Both entities are quite simple. The <i>Author</i> entity has an id, a version, a first name and a last name. The <i>Book</i> entity has an id, a version, a title and a reference to the <i>Author</i>. To avoid unnecessary complexity, each <i>Book</i> was written by only one <i>Author</i>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiESFtMwbm6Y35XD6NUMUhJUbvUhOX5tnEuzrtWUeDNADDg2H3c77-1NFSB_j0NKVHRpgmw2HN2CTzkSWkvcGyEtN8PdX4BBI6MZJgeRWPn8PwGdAcz3fQLD88w_rh8fzJvlYxuhS5PJdT8/s1600/class+diagram+entities.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiESFtMwbm6Y35XD6NUMUhJUbvUhOX5tnEuzrtWUeDNADDg2H3c77-1NFSB_j0NKVHRpgmw2HN2CTzkSWkvcGyEtN8PdX4BBI6MZJgeRWPn8PwGdAcz3fQLD88w_rh8fzJvlYxuhS5PJdT8/s1600/class+diagram+entities.png" width="550" /></a></div>
As we want to map our query results to a value object, we need an additional class called <i>BookValue</i> with an id, a version, a title and the name of the author.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhwX5NFw_MZ-jJPNbpIu5yqjm3gCrn-m2M_FVT8aeLvaQ7nKlvvv14eZDqvOTM-AxRa8PDLToa52n22LKJP7FXX_A9H3IoA5DhuWVDty8zX-B0HG3usIsCRiNcqypodVos8qkWq0cEsvDQ/s1600/class+diagram+BookValue.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhwX5NFw_MZ-jJPNbpIu5yqjm3gCrn-m2M_FVT8aeLvaQ7nKlvvv14eZDqvOTM-AxRa8PDLToa52n22LKJP7FXX_A9H3IoA5DhuWVDty8zX-B0HG3usIsCRiNcqypodVos8qkWq0cEsvDQ/s1600/class+diagram+BookValue.png" height="342" width="400" /></a></div>
<br />
I used <a href="http://wildfly.org/" rel="nofollow" target="_blank">Wildfly</a> 8.2 with <a href="http://hibernate.org/orm/" rel="nofollow" target="_blank">Hibernate</a> 4.3.7 to test the examples in this series. But as these are standard JPA features, you should be able to use them with all other JPA 2.1 implementations, e.g. with the <a href="https://glassfish.java.net/" rel="nofollow" target="_blank">GlassFish 4.1</a> application server which uses <a href="https://eclipse.org/eclipselink/" rel="nofollow" target="_blank">EclipseLink</a>.<br />
You can find the source code on my <a href="https://github.com/thjanssen/ResultMapping" rel="nofollow" target="_blank">github account</a>.<br />
<br />
<h2>
How to map to a value object</h2>
</div>
<div>
Selecting entities and returning a tree of objects to the caller is not always the best approach. The caller often needs only a subset of the provided information and a specific value object would be much more efficient. For these situations, JPQL supports constructor expressions that can be specified in the select part of the JPQL query and define the constructor call for each selected record.</div>
<div>
Up to JPA 2.1, this feature was missing to the SQL result mapping. </div>
<br />
<div style="text-align: center;">
<a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank"><b>Learn more about the features added in JPA 2.1 and</b></a></div>
<div style="text-align: center;">
<b><a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">get the free cheat sheet "What's new in JPA 2.1"</a></b></div>
<div style="text-align: left;">
<br /></div>
The <i>BookValue</i> in our example could be used in a client to show some information about a <i>Book</i> and the name of the <i>Author</i>. As long as we only need the name of the <i>Author</i>, there is no need to select and transfer the whole <i>Author </i>entity. It is more efficient to use an object of <i>BookValue</i> and select the name of the <i>Author</i> in the query.
<br />
<br />
<script src="https://gist.github.com/thjanssen/7fa350671de484a0127a.js?file=bookValue.sql"></script>>
<br />
In the next step, we need to define a mapping that uses the query result to call the constructor of the <i>BookValue</i>. This is done similar to the mappings we created before with the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html" rel="nofollow" target="_blank"><i>@SqlResultSetMapping</i></a> annotation. The mapping shown in the following code snippet consists of a name and a <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ConstructorResult.html" rel="nofollow" target="_blank">@ConstructorResult</a></i> annotation.<br />
<br />
<script src="https://gist.github.com/thjanssen/7fa350671de484a0127a.js?file=BookValueMapping.java"></script>
The name of the mapping, <i>BookValueMapping</i> in this example, will later be used to tell the <i>EntityManager </i>which mapping to use. The <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ConstructorResult.html" rel="nofollow" target="_blank">@ConstructorResult</a></i> annotation defines the constructor call for a given target class. This is the <i>BookValue </i>in our example. The array of <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ColumnResult.html" rel="nofollow" target="_blank">@ColumnResult</a></i> annotations defines the columns of the query result that will be used as constructor parameters with their type and order. The type attribute is optional and you only need to provide it, if the type of the column is different to the type of the constructor parameter. In this case, the default types of the id and version columns are <i>BigInteger </i>and need to be converted to <i>Long</i>.<br />
<br />
Similar to the <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">mapping of multiple entities</a>, the <i>classes</i> attribute of the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html" rel="nofollow" target="_blank"><i>@SqlResultSetMapping</i></a> accepts an array of <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ConstructorResult.html" rel="nofollow" target="_blank">@ConstructorResult</a></i> annotations. If the mapping maps to multiple value objects or entities, each column can be used multiple times.<br />
<br />
And like all the mapping definitions before, also the constructor result mapping can be defined in a mapping XML file. The easiest way to do this is to use the default mapping file called <i>orm.xml</i> that will be automatically used, if it is added to the <i>META-INF</i> directory of the jar file.<br />
<br />
<script src="https://gist.github.com/thjanssen/7fa350671de484a0127a.js?file=bookValueMapping.xml"></script>
The usage of the constructor mapping is identical to the other SQL result set mappings. We need to provide it to the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.String)" rel="nofollow" target="_blank"><i>createNativeQuery(String sqlString, String resultSetMapping)</i></a> method of the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html" rel="nofollow" style="font-style: italic;" target="_blank">EntityManager</a> and we get a <i>List<BookValue></i>.<br />
<br />
<script src="https://gist.github.com/thjanssen/7fa350671de484a0127a.js?file=UseBookValueMapping.java"></script>
<h2>
Conclusion</h2>
After we started with <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">simple result mappings</a> in the first post and created <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">more complex ones</a> in the second post, we now had a look at constructor result mappings that were <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">introduced with JPA 2.1</a>.<br />
These provide a functionality similar to the JPQL constructor expressions and map the result of a query to constructor calls. The mapping can be defined via XML or a <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html" rel="nofollow" target="_blank"><i>@SqlResultSetMapping</i></a> annotation and one or more <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ConstructorResult.html" rel="nofollow" target="_blank">@ConstructorResult</a></i> annotations.<br />
<br />
Here is again a list of the previous posts of the series in case you missed one of them:<br />
<ul>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">Result Set Mapping: The Basics</a></li>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">Result Set Mapping: Complex Mappings</a></li>
<li><b>Result Set Mapping: Constructor Result Mappings</b></li>
<li>Result Set Mapping: Hibernate specific features <i>(coming soon)</i></li>
</ul>
<div>
In the next post, we will have a look at some Hibernate specific features to map query results. Make sure to <a href="https://www.blogger.com/blogger.g?blogID=5355106262763766945#cheatSheet">subscribe to my mailing list</a> so you don't miss the following posts and to <a href="https://www.blogger.com/blogger.g?blogID=5355106262763766945#cheatSheet">grab your free "What's new in JPA 2.1" cheat sheet</a>.</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-29603536447120111272015-04-20T05:31:00.000+02:002015-04-20T05:31:52.469+02:00Java Weekly 17/15: Java stats, caching and JPA productivityThe 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyo5RZhBdWpaPmGLaEXEuFgb0yIuMIi_lc7uvKfZTjJH40_keVXYJ-GOsU7wxuBvZqqZw9Fun2_BdDYD6p2adB_6oLrGDH1oPbJQfsIlSp1Kzp-M0IhSgEWF7CHQgZ2MuUm-4pGBZhf5mO/s1600/Java+Weekly+17-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
Do you know which Java version is currently used the most? The people from Plumbr know, at least for their customer base. They collected some interesting statistics from the 758 JVMs monitored by their software: <a href="https://plumbr.eu/blog/java/java-version-statistics-2015-edition" rel="nofollow" target="_blank">Java version statistics: 2015 edition</a>.<br />
Hell, there are a lot of legacy JVMs out there!<br />
<br />
<br />
Due to the EOL of Java 7, the migration to Java 8 is currently a hot topic. And as soon as the migration is done, there is time to do some refactoring and use all these handy new features like streams and lambdas, isn't it?<br />
Fabrizio Giudici wrote a blog post showing the refactoring steps in one of his projects and how the code becomes more readable: <a href="http://tidalwave.it/fabrizio/blog/a-real-world-example-of-refactoring-with-java-8-streams/" rel="nofollow" target="_blank">A real-world example of refactoring with Java 8 streams</a>.<br />
<br />
<br />
<h2>
Java EE</h2>
If you read Java Weekly regularly (what you should ;-) ), you probably know Vlad Mihalcea and his excellent Hibernate articles. Not long ago, he started to write about caching in Hibernate:<br />
<br />
<ul>
<li><a href="http://vladmihalcea.com/2015/04/16/things-to-consider-before-jumping-to-enterprise-caching/" rel="nofollow" target="_blank">Things to consider before jumping to enterprise caching</a></li>
<li><a href="http://vladmihalcea.com/2015/04/09/how-does-hibernate-store-second-level-cache-entries/" rel="nofollow" target="_blank">How does Hibernate store second-level cache entries</a></li>
</ul>
<br />
You should definitely have a look at these articles, if you want to learn more about caching in Hibernate!<br />
<br />
<br />
Caching is also the topic of the next link. The team from ConSol Labs created a nice introduction to<br />
<a href="https://labs.consol.de/java-caches/" rel="nofollow" target="_blank">Java Caches: Ehcache, Hazelcast, Infinispan</a>. After a short introduction to their example application, they talk about local and shared caches and show how to implement them with Ehcache, Hazelcast and Infinispan.<br />
<br />
<br />
Last weeks <a href="http://www.thoughts-on-java.org/2015/04/java-weekly-1615.html" target="_blank">Java Weekly 16/15</a> featured a post about the <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">basics of JPA result set mappings</a> and how they can be used to map the result of native queries to entities. This week we have the second post of this series, which shows how to use more complex mappings to <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">map the result to multiple entities and handle additional columns</a>.<br />
<br />
<br />
Matti Tahvonen wrote some nice articles on how to be more productive when using JPA. In his first post, he showed how to <a href="http://jaxenter.com/top-3-jpa-productivity-boosters-for-java-ee-developers-spring-data-116126.html" rel="nofollow" target="_blank">use Spring Data in an Java EE environment</a> and in the second, he used <a href="http://jaxenter.com/top-3-jpa-productivity-boosters-for-java-ee-developers-deltaspike-data-116136.html" rel="nofollow" target="_blank">DeltaSpike Data</a>. Both frameworks provide easy ways to generate the most common DAO methods for an entity and can save you quite some work when writing CRUD use cases.<br />
<br />
<br />
<h2>
Recordings</h2>
Josh Long gave an interesting talk about building microservices with Spring Boot at the vJUG. As always, the talk was recorded and Oleg Shelajev created a nice write-up and a short interview with Josh: <a href="http://zeroturnaround.com/rebellabs/building-bootiful%C2%AD-microservices-with-spring-cloud-by-josh-long/" rel="nofollow" target="_blank">Building “Bootiful” Microservices with Spring Cloud by Josh Long</a>.<br />
<br />
<br />
<h2>
Upcoming online events</h2>
<div>
This is a busy week at the JBoss virtual user group:</div>
<div>
<br /></div>
<div>
Erik Jan de Wit will talk about <a href="http://www.meetup.com/JBoss-User-Group-Worldwide/events/219852806/" rel="nofollow" target="_blank">Building Cross Platform Applications with Cordova and AeroGear</a> on Monday 20th at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Tuesday+April+20th+2015" rel="nofollow" target="_blank">5pm UTC</a> at the virtual JUG.<br />
<br />
And on Tuesday 21st at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Tuesday+April+21st+2015" rel="nofollow" target="_blank">5 pm UTC</a>, Kabir Khan will show <a href="http://www.meetup.com/JBoss-User-Group-Worldwide/events/220111522/" rel="nofollow" target="_blank">how to contribute to the Wildfly project</a>.</div>
<div>
<br /></div>
<div>
<div style="margin: 0px;">
<br /></div>
</div>
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-72167570488255829302015-04-14T05:37:00.000+02:002015-04-19T08:02:13.713+02:00Result Set Mapping: Complex MappingsThis is the second part of my SQL result set mappings series. We had a look at some basic result type mappings in the first post <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">Result Set Mapping: The Basics</a>. In this one, we will define more complex mappings that can map a query result to multiple entities and handle additional columns that cannot be mapped to a specific entity.<br />
<ul>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">Result Set Mapping: The Basics</a></li>
<li><b>Result Set Mapping: Complex Mappings</b></li>
<li>Result Set Mapping: Constructor Result Mappings <i>(coming soon)</i></li>
<li>Result Set Mapping: Hibernate specific features <i>(coming soon)</i></li>
</ul>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZNguaI8Rm6TY14samR5EkUdmYuBJPWemE9qGbvMKlLX2hjor1hN3q588WMS-_tOvGPXfElLs2QGyNGuZXSGYxNo9AhZwgQza0dGgUeouh_7uhYO0EewBIU9-9yVW7DGrVCMWgOfpzsvFa/s1600/Result+Set+Mappings+-+Complex+(1).png" width="100%" />
<br />
<a name='more'></a><h2>
The example</h2>
Before we dive into the more complex mappings, lets have a look at the entity model that we will use for the examples. We used the <i>Author </i>entity with an id, a version, a first name and a last name already in the <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">first post</a> of this series. For the more complex mappings, we need the additional <i>Book </i>entity which has an id, a version, a title and a reference to the <i>Author</i>. To keep it simple, each book is only written by one author.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiESFtMwbm6Y35XD6NUMUhJUbvUhOX5tnEuzrtWUeDNADDg2H3c77-1NFSB_j0NKVHRpgmw2HN2CTzkSWkvcGyEtN8PdX4BBI6MZJgeRWPn8PwGdAcz3fQLD88w_rh8fzJvlYxuhS5PJdT8/s1600/class+diagram+entities.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiESFtMwbm6Y35XD6NUMUhJUbvUhOX5tnEuzrtWUeDNADDg2H3c77-1NFSB_j0NKVHRpgmw2HN2CTzkSWkvcGyEtN8PdX4BBI6MZJgeRWPn8PwGdAcz3fQLD88w_rh8fzJvlYxuhS5PJdT8/s1600/class+diagram+entities.png" width="550" /></a></div>
I used <a href="http://wildfly.org/" rel="nofollow" target="_blank">Wildfly</a> 8.2 with <a href="http://hibernate.org/orm/" rel="nofollow" target="_blank">Hibernate</a> 4.3.7 to test the examples in this series. But as these are standard JPA features, you should be able to use them with every JPA 2.1 implementation, e.g. with the <a href="https://glassfish.java.net/" rel="nofollow" target="_blank">GlassFish 4.1</a> application server which uses <a href="https://eclipse.org/eclipselink/" rel="nofollow" target="_blank">EclipseLink</a>.<br />
You can find the source code on my <a href="https://github.com/thjanssen/ResultMapping" rel="nofollow" target="_blank">github account</a>.<br />
<br />
<h2>
How to map multiple entities</h2>
In real life applications we often select multiple entities with one query to avoid the additional queries that would be required to <a href="http://www.thoughts-on-java.org/2014/12/5-ways-to-initialize-lazy-relations.html" rel="nofollow" target="_blank">initialize lazy relations</a>. If we do this with a native query or a stored procedure call, we get a <i>List<Object[]></i> instead of entities. We then need to provide a custom mapping that tells the <i>EntityManager</i> to which entities the <i>Object[]</i> shall be mapped and how this is done.<br />
<br />
In our example we could define a query that returns books and its author in one query.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=booksWithAuthor.sql"></script>
<br />
As the <i>Author</i> and the <i>Book</i> table both have an <i>id</i> and a <i>version</i> column, we need to rename them in the SQL statement. I decided to rename the <i>id</i> and <i>version</i> column of the <i>Author</i> to <i>authorId</i> and <i>authorVersion</i>. The columns of the <i>Book</i> stay unchanged.<br />
<br />
OK, so how do we define a SQL result set mapping that transforms the returned <i>List</i> of <i>Object[]</i> to a <i>List</i> of fully initialized <i>Book</i> and <i>Author</i> entities?<br />
The mapping definition looks similar to the custom mapping that we defined in the post about <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">basic result set mappings</a>. As in the previously discussed mapping, the <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html" rel="nofollow" target="_blank">@SqlResultMapping</a></i> defines the name of the mapping that we will use to reference it later on. The main difference here is, that we provide two <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityResult.html" rel="nofollow" target="_blank">@EntityResult</a></i> annotations, one for the <i>Book</i> and one for the <i>Author</i> entity. The <i>@EntityResult</i> looks again similar to the previous mapping and defines the entity class and a list of <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/FieldResult.html" rel="nofollow" target="_blank"><i>@FieldResult</i></a> mappings.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=BookAuthorMapping.java"></script>
<br />
If you don't like to add such a huge block of annotations to your entity, you can also define the mapping in an XML file. As <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">described before</a>, the default mapping file is called <i>orm.xml</i> and will be automatically used, if it is added to the <i>META-INF</i> directory of the jar file.<br />
The mapping definition itself looks similar to the already described annotation based mapping definition.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=bookAuthorMapping.xml"></script>
<br />
Now we have a custom result set mapping definition, that defines the mapping between our query result and the <i>Book</i> and <i>Author</i> entity. If we provide this to the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.String)" rel="nofollow" target="_blank"><i>createNativeQuery(String sqlString, String resultSetMapping)</i></a> method of the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html" rel="nofollow" target="_blank"><i>EntityManager</i></a>, we get a <i>List<Object[]></i>.<br />
<br />
OK, that might not look like what we wanted to achieve in the first place. We wanted to get rid of these <i>Object[]</i>. If we have a more detailed look at the <i>Object</i>s in the array, we see that these are no longer the different columns of the query but the <i>Book</i> and <i>Author</i> entities. And as the <i>EntityManager</i> knows that these two entities are related to each other, the relation on the <i>Book</i> entity is already initialized.<br /><br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=UseBookAuthorMapping.java"></script>
<br />
<h2>
How to map additional columns</h2>
Another very handy feature is the mapping of additional columns in the query result. If we want to select all <i>Author</i>s and their number of <i>Book</i>s, we can define the following query.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=additionalColumn.sql"></script>
<br />
So how do we map this query result to an <i>Author</i> entity and an additional <i>Long</i> value?<br />
That is quite simple, we just need to combine a mapping for the <i>Author</i> entity with an additional <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ColumnResult.html" rel="nofollow" target="_blank">@ColumnResult</a></i> definition. The mapping of the <i>Author</i> entity has to define the mapping of all columns, even if we do not change anything as in the example below. The <i>@ColumnResult</i> defines the name of the column that shall be mapped and can optionally specify the Java type to which it shall be converted. I used it to convert the <i>BigInteger</i>, that the query returns by default, to a <i>Long</i>.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=AuthorBookCountMapping.java"></script>
<br />
As before, this mapping can also be defined with a similar looking XML configuration.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=authorBookCountMapping.xml"></script>
<br />
If we use this mapping in the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.String)" rel="nofollow" style="font-style: italic;" target="_blank">createNativeQuery(String sqlString, String resultSetMapping)</a> of the <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html" rel="nofollow" target="_blank">EntityManager</a></i>, we get a <i>List<Object[]></i> that contains the initialized <i>Author</i> entity and the number of her/his <i>Book</i>s as a <i>Long</i>.<br />
<br/>
<script src="https://gist.github.com/thjanssen/fb8a1703de6d6d94e0a9.js?file=UseAuthorBookCountMapping.java"></script>
<br />
This kind of mapping comes quite handy, if your query becomes complex and the result has no exact mapping to your entity model. Reasons for this can be additional attributes calculated by the database, as we did in the example above, or queries that select only some specific columns from related tables.<br />
<br />
<h2>
Conclusion</h2>
<div>
In the first post of this series, we had a look at some <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">basic ways to map query results to entities</a>. But this is often not sufficient for real world applications. Therefore we created some more complex mappings in this post that:</div>
<div>
<ul>
<li>can map a query result to multiple entities by annotating multiple <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityResult.html" rel="nofollow" target="_blank">@EntityResult</a></i> annotations and</li>
<li>can handle columns, that are not part of the entity, with the <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/ColumnResult.html" rel="nofollow" target="_blank">@ColumnResult</a></i> annotation.</li>
</ul>
<div>
In the following posts, we will use the constructor result mapping, that was <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">introduced in JPA 2.1</a> and have a look at some Hibernate specific features:</div>
<div>
<ul>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" target="_blank">Result Set Mapping: The Basics</a></li>
<li><b>Result Set Mapping: Complex Mappings</b></li>
<li>Result Set Mapping: Constructor Result Mappings <i>(coming soon)</i></li>
<li>Result Set Mapping: Hibernate specific features <i>(coming soon)</i></li>
</ul>
</div>
<div>
Make sure to <a href="https://www.blogger.com/blogger.g?blogID=5355106262763766945#cheatSheet">subscribe to my mailing list</a> so you don't miss the following posts and to <a href="https://www.blogger.com/blogger.g?blogID=5355106262763766945#cheatSheet">grab your free "What's new in JPA 2.1" cheat sheet</a>.</div>
</div>Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-68670998477821545412015-04-13T05:18:00.000+02:002015-04-13T05:18:07.381+02:00Java Weekly 16/15: Microservice design patterns, scopes for JBatch, Java 8 concurrency, IoT with Java ME 8 and moreThe 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicPYDZJvnSDCJD2WVIfuDoaYB1ubX_RGu9Z4SKKYlwtTfHt-CtyN1Dnn7PtWidiQCMx0nC0l3HdxrEx4gxU7-vqRAdfRB4aQAIfQFrtt0broVU3rFFFhJi2n_qClnkGN-5hCeKJ97sRTmc/s1600/Java+Weekly+16-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
Benjamin Winterberg posted the first part of his Java 8 Concurrency tutorial. In this post, he provides a very detailed explanation of <a href="http://winterbe.com/posts/2015/04/07/java8-concurrency-tutorial-thread-executor-examples/" rel="nofollow" target="_blank">Threads and Executors</a> with lots of code samples.<br />
<br />
<br />
Upgrading your existing applications to Java 8 is tempting, if you have a look at all the new features and the end of public updates for Java 7. But upgrading large system is not an easy task. The HubSpot did it anyways and it took them nearly 5 month. Jonathan Haber wrote an interesting post about the problems they had to face: <a href="http://product.hubspot.com/blog/upgrading-to-java-8-at-scale" rel="nofollow" target="_blank">Upgrading to Java 8 at Scale</a>.<br />
<br />
<br />
<h2>
Java EE</h2>
Do you use <i>@Dependent</i> scope for your JBatch artifacts?<br />
Mark Struberg explains in his post <a href="https://struberg.wordpress.com/2015/04/09/the-right-scope-for-jbatch-artifacts/" rel="nofollow" target="_blank">The right Scope for JBatch Artifacts</a> why you should do that to avoid working with wrong batch properties.<br />
<br />
<br />
Using native SQL queries or stored procedures with JPA is a great way to use the full power of your database. But working with the <i>List<Object></i> that you get as a result can be tedious. SQL result set mappings provide an easy solution and map the <i>List</i> into entities and value objects. This first post of an upcoming series shows how to create basic mappings: <a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html" rel="nofollow" target="_blank">Result Set Mapping: The Basics</a>.<br />
<br />
<br />
<h2>
Microservices</h2>
Do you think about using microservice based architecture for your project and wonder how to combine all these services into one application?<br />
Don't worry, Arun Gupta got you covered and wrote a great post about <a href="http://blog.arungupta.me/microservice-design-patterns/" rel="nofollow" target="_blank">microservice design patterns</a>. Read his post to learn more about patterns like aggregator, proxy, branch or asynchronous messaging.<br />
<br />
<br />
<h2>
This and that</h2>
<div>
RebelLabs donates 0.50$ for each completed <a href="https://rebellabs.typeform.com/to/yhPzjG" rel="nofollow" target="_blank">Java performance survey</a> to a charity that provides support dogs for children with disabilities. You don't have to provide any personal information (you can skip those questions) and only need to answer a few questions about how your team analyzes application performance. So, if you haven't already done that, take a few minutes and fill out the survey. I already did it and it was done quickly. </div>
<div>
Here is the link again: <a href="https://rebellabs.typeform.com/to/yhPzjG" rel="nofollow" target="_blank">RebelLabs' Java Performance Survey 2015</a>.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Jose Cruz started an interesting series about Java ME 8. In this first post, he shows how to use Java ME 8 on a Raspbery Pi to work with sensors for distance measurement, flame and movement detection: <a href="http://www.oracle.com/technetwork/articles/java/cruz-gpio-2295970.html" rel="nofollow" target="_blank">Java ME 8 + Raspberry Pi + Sensors = IoT World (Part 1)</a>. </div>
<div>
I am looking forward to the next part of this series.</div>
<div>
<br /></div>
<br />
Writing good tests is not easy. One of the problems is to describe the method under test and its state in a clear way without creating method names that are way too long.<br />
Petri Kainulainen shows in his recent post <a href="http://www.petrikainulainen.net/programming/testing/writing-clean-tests-small-is-beautiful/" rel="nofollow" target="_blank">Writing Clean Tests – Small Is Beautiful</a> how this could be solved with nested classes and the <i>NestRunner</i> for junit.<br />
<br />
<br />
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait until then? Follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> to get updates during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-4403985470988832432015-04-07T05:16:00.000+02:002015-04-19T06:22:50.504+02:00Result Set Mapping: The BasicsQuite often JPQL is not powerful enough to perform the queries we need in real world projects. In general, this is not an issue because JPA is designed as a leaky abstraction and we can use the full potential of SQL by using native queries or calling stored procedures.<br />
The only downside is, that these queries return a <i>List</i> of <i>Object[]</i> instead of the mapped entities and value objects we are used to work with. Each <i>Object[]</i> contains one record returned by the database. We then need to iterate through the array, cast each <i>Object</i> to its specific type and map them to our domain model. This creates lots of repetitive code and type casts as you can see in the following example.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cb1061a30b17e2288223.js?file=Streams.java"></script>
<br />
It would be more comfortable, if we could tell the <i>EntityManager</i> to map the result of the query into entities or value objects as it is the case for JPQL statements. The good news is, JPA provides this functionality. It is called SQL result set mapping and we will have a detailed look at it during this series:<br />
<ul>
<li><b>Result Set Mapping: The Basics</b></li>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">Result Set Mapping: Complex Mappings</a></li>
<li>Result Set Mapping: Constructor Result Mappings <i>(coming soon)</i></li>
<li>Result Set Mapping: Hibernate specific features <i>(coming soon)</i></li>
</ul>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-EzUFGNEhJ-0NBJr_O34QRakZcHKSe6hkzhDeSpbyECYbuC8k7WqETYbPrghuaqOZLUt6ZJCe8qhfpwxyDOSC1vnDbiJQPG7Ll25njwteEzYXJm4qOp_ktWl88jNT0EXgYL8LJ7x2n7iC/s1600/Result+Set+Mappings+-+Basics+(1).png" width="100%" />
<br />
<a name='more'></a><h2>
The example</h2>
We only need a simple <i>Author </i>entity with an id, a version, a first name and a last name for this post.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhL3cqOjvGx2HgQZsLkM6I9MiGcM3llcnet9SaGPGDKyyfLT6x5Mia9JPxkUh507M36qtdwKSf415IQKVWQiI8pcoBVtzRBE6YM86IUi-kwlpxfBY-xyEy60tFfjRTixdg28gwx7lBXJQ2D/s1600/class+diagram+Author.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhL3cqOjvGx2HgQZsLkM6I9MiGcM3llcnet9SaGPGDKyyfLT6x5Mia9JPxkUh507M36qtdwKSf415IQKVWQiI8pcoBVtzRBE6YM86IUi-kwlpxfBY-xyEy60tFfjRTixdg28gwx7lBXJQ2D/s1600/class+diagram+Author.png" height="400" width="260" /></a></div>
<br />
I used <a href="http://wildfly.org/" rel="nofollow" target="_blank">Wildfly</a> 8.2 with <a href="http://hibernate.org/orm/" rel="nofollow" target="_blank">Hibernate</a> 4.3.7 to test the examples in this series. But as these are standard JPA features, you should be able to use them with every JPA 2.1 implementations, like <a href="https://eclipse.org/eclipselink/" rel="nofollow" target="_blank">EclipseLink</a>.<br />
You can find the source code on my <a href="https://github.com/thjanssen/ResultMapping" rel="nofollow" target="_blank">github account</a>.<br />
<br />
<h2>
How to use the default mapping</h2>
The easiest way to map a query result to an entity is to provide the entity class as a parameter to the <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.Class)" rel="nofollow" target="_blank">createNativeQuery(String sqlString, Class resultClass)</a></i> method of the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html" rel="nofollow" target="_blank"><i>EntityManager</i></a> and use the default mapping. The following snippet shows how this is done with a very simple query. In a real project, you would use this with a stored procedure or a very complex SQL query.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cb1061a30b17e2288223.js?file=defaultMapping.java"></script>
<br />
The query needs to return all properties of the entity and the JPA implementation (e.g. <a href="http://hibernate.org/orm/" rel="nofollow" target="_blank">Hibernate</a>) will try to map the returned columns to the entity properties based on their name and type. If that is successful, the <i>EntityManager</i> will return a list of fully initialized <i>Author </i>entities that are managed by the current persistence context. So the result is the same as if we had used a JPQL query, but we are not limited to the small feature set of JPQL.<br />
<br />
<h2>
How to define a custom mapping</h2>
While this automatic mapping is useful and easy to define, it is often not sufficient. If we perform a more complex query or call a stored procedure, the names of the returned columns might not match the entity definition. In these cases we need to define a custom result mapping. This needs to define the mapping for all entity properties, even if the default mapping cannot be applied to only one property.<br />
<br />
Let's have a look at our example and change the query we used before and rename the <i>id </i>column to <i>authorId</i>:<br />
<br/>
<script src="https://gist.github.com/thjanssen/cb1061a30b17e2288223.js?file=query.sql"></script>
The default mapping to the <i>Author</i> entity will not work with this query result because the names of the selected columns and the entity properties do not match. We need to define a custom mapping for it. This can be done with annotations or in a mapping file (e.g. <i>orm.xml</i>).<br />
<br />
The following code snippet shows how to define the result mapping with the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html" rel="nofollow" target="_blank"><i>@SqlResultSetMapping</i></a> annotation. The mapping consists of a name and an <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityResult.html" rel="nofollow" target="_blank">@EntityResult</a></i> definition. The name of the mapping, <i>AuthorMapping</i> in this example, will later be used to tell the <i>EntityManager</i> which mapping to use. The <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityResult.html" rel="nofollow" target="_blank">@EntityResult</a></i> defines the entity class to which the result shall be mapped and an array of <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/FieldResult.html" rel="nofollow" target="_blank">@FieldResult</a></i> which defines the mapping between the column name and the entity property. Each <i>@FieldResult</i> gets the name of the property and the column name as a parameter.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cb1061a30b17e2288223.js?file=Mapping.java"></script>
<br />
As Java EE 7 is based on Java 7, there is no support for <a href="https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html" rel="nofollow" target="_blank">repeatable annotations</a>. Therefore you need to place your <i>@SqlResultSetMapping</i> annotations within a <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMappings.html" rel="nofollow" target="_blank">@SqlResultMappings</a></i> annotation, if you want to define more than one mapping at an entity.<br />
<br />
If you don't like to add huge blocks of annotations to your entities, you can define the mapping in an XML mapping file. The default mapping file is called <i>orm.xml</i> and will be used automatically, if it is added to the <i>META-INF</i> directory of the jar file.<br />
As you can see below, the mapping is very similar to the annotation based mapping that we discussed before. I named it <i>AuthorMappingXml</i> to avoid name clashes with the annotation based mapping. In a real project, you don't need to worry about this, because you would normally use only one of the two described mappings.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cb1061a30b17e2288223.js?file=mapping.xml"></script>
<br />
OK, so now we have defined our own mapping between the query result and the <i>Author</i> entity. We can now provide the name of the mapping instead of the entity class as a parameter to the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.String)" rel="nofollow" target="_blank"><i>createNativeQuery(String sqlString, String resultSetMapping)</i></a> method. In the code snippet below, I used the annotation defined mapping.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cb1061a30b17e2288223.js?file=UseMapping.java"></script>
<br />
<h2>
Conclusion</h2>
In this first post of the series, we had a look at two basic ways to map the query result to an entity:<br />
<ol>
<li>If the names and the types of the query result match to the entity properties, we only need to provide the entity class to the <i><a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.Class)" rel="nofollow" target="_blank">createNativeQuery(String sqlString, Class resultClass)</a></i> method of the <i>EntityManager</i> to use the default mapping.</li>
<li>If the default mapping cannot be applied to the query result, we can use XML or the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html" rel="nofollow" target="_blank"><i>@SqlResultSetMapping</i></a> annotation to define a custom mapping between the columns of the query result and the properties of an entity. The name of the mapping can then be provided to the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery(java.lang.String, java.lang.String)" rel="nofollow" target="_blank"><i>createNativeQuery(String sqlString, String resultSetMapping)</i></a> method.</li>
</ol>
<div>
The mappings described in this post were quite simple. In the following posts of this series, we will have a look at more complex mappings that can handle more than one entity and additional columns or that can map to value objects instead of entities:<br />
<ul>
<li><b>Result Set Mapping: The Basics</b></li>
<li><a href="http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html" target="_blank">Result Set Mapping: Complex Mappings</a></li>
<li>Result Set Mapping: Constructor Result Mappings <i>(coming soon)</i></li>
<li>Result Set Mapping: Hibernate specific features <i>(coming soon)</i></li>
</ul>
<div>
Make sure to <a href="https://www.blogger.com/blogger.g?blogID=5355106262763766945#cheatSheet">subscribe to my mailing list</a> so you don't miss the following posts and to <a href="https://www.blogger.com/blogger.g?blogID=5355106262763766945#cheatSheet">grab your free "What's new in JPA 2.1" cheat sheet</a>.</div>
</div>Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-52458084062008640812015-04-06T07:00:00.000+02:002015-04-06T07:00:00.667+02:00Java Weekly 15/15: GC tuning, HTTP2, good Javadoc, MVC 1.0 early draftThe 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIR_70Ip6IxPgvU4zEXyMRX9NIAm-YMmN9f3nYUHzM2BrfedouMCHlElMa5WIWUeElzko90U25ELHtfSevRQHa0fox-o3prgryaG1Svwd29hpWENfYqgNqewAcssEmCqUqsd7o55f7juN0/s1600/Java+Weekly+15-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
The Garbage Collector (GC) is one of the most common causes for slow application performance. Haim Yadid wrote a great post explaining how to analyze GC pauses and which four strategies you can use to reduce them: <a href="http://blog.takipi.com/java-performance-tuning-how-to-get-the-most-out-of-your-garbage-collector" rel="nofollow" target="_blank">Java Performance Tuning: Getting the Most Out of Your Garbage Collector</a>.<br />
<br />
<br />
Writing JavaDoc is useful! Everyone of us knows that. But that doesn't mean, that we always write it. Thierry Wasylczenko wrote a nice article reminding us why we should take more care about our Javadoc and explaining the 9 tags that can be used to <a href="http://zeroturnaround.com/rebellabs/reasons-tips-and-tricks-for-better-java-documentation/" rel="nofollow" target="_blank">create a good documentation</a> of our source code.<br />
<br />
<br />
<h2>
Java EE 8</h2>
The JSR 371 - MVC 1.0 is the first Java EE 8 related JSR to <a href="https://blogs.oracle.com/theaquarium/entry/jsr371_edr" rel="nofollow" target="_blank">enter the Early Draft Review</a>. David Delabassee created a short write-up about it with the slides of his JavaLand presentation and several other useful links to review and test the specification.<br />
<br />
<br />
<h2>
HTTP/2</h2>
Beginning with 9.0.0.Beta1, the Wildfly application server <a href="http://undertow.io/blog/2015/03/26/HTTP2-In-Wildfly.html" rel="nofollow" target="_blank">will support HTTP/2</a>. If you want to learn more about Multiplexing, Header Compression and Server Push, you can have a look at this great <a href="http://undertow.io/blog/2015/04/27/An-in-depth-overview-of-HTTP2.html" rel="nofollow" target="_blank">in depth overview of HTTP/2</a> by the Wildfly team.<br />
<br />
<br />
<h2>
This and that</h2>
Heinz Kabutz published his recent issue of the Java Specialists' Newsletter. This time he talks about <a href="https://iw127.infusionsoft.com/app/hostedEmail/1694950/b226240b016774e1/T" rel="nofollow" target="_blank">how to become a Campion Programmer</a>.<br />
<br />
<br />
Dan McKinley wrote a great article on choosing technology. His recommendation is to make mindful choices. There should always be an issue with the current technology stack that you want to solve with the new one: <a href="http://mcfunley.com/choose-boring-technology" rel="nofollow" target="_blank">Choose Boring Technology</a>.<br />
<br />
<br />
<h2>
Recordings</h2>
Markus Eisele gave an interesting talk about <a href="http://blog.eisele.net/2015/04/architecting-large-enterprise-java-projects.html" rel="nofollow" target="_blank">architecting large enterprise Java projects</a> and showed us that Java EE is quite a good fit to implement applications based on a microservice architecture.<br />
<br />
<br />
<h2>
Upcoming online events</h2>
<div>
Josh Long will talk about <a href="http://www.meetup.com/virtualJUG/events/221218834/" rel="nofollow" target="_blank">Building "Bootiful" Microservices with Spring Cloud</a> on Thursday 9th at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+April+9th+2015" rel="nofollow" target="_blank">5pm UTC</a> at the virtual JUG.</div>
<div>
<div style="margin: 0px;">
<br />
<br /></div>
</div>
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-18036896570205482162015-04-01T21:36:00.000+02:002015-04-01T21:36:02.025+02:00Java Weekly: April Fools EditionNormally, I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> to collect the most interesting Java related links. But today I prepared a special April Fool Edition for you. I hope you enjoy it.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7x8aO86X5NYsvu-ZoRQvdw6AjsYukdjhnK8zIhbsDHXfrT0hVm8OKlHPQeYjvkCVln4g-EFc975oYftKjFf6pOWTFytgjudgxy6993iCsPlLTxd-W_0wSDc-Yt7s3XdOdn9PACF0BLpvk/s1600/April+Fool+Edition.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<br />
Researchers at the CERN institute discovered the <a href="http://home.web.cern.ch/about/updates/2015/04/cern-researchers-confirm-existence-force" rel="nofollow" target="_blank">first evidence for <i>the</i> Force</a>. They also found several practical applications for it, like influencing minds, lifting heavy things or reheating coffee.<br />
<br />
<br />
Neo Technology announced the newest generation of their <a href="http://neo4j.com/blog/neo-technology-previews-latest-matrix-edition-neo4j-featuring-braindump-import-facilities/" rel="nofollow" target="_blank">graph database with codename "Matrix"</a>. This revolutionary software massively improves the capabilities of existing graph databases and is now able to process partial and full braindumps of live subjects. I can't wait to give this database a try!<br />
<br />
<br />
The jOOQ engineering team identified backwards-compatibility issues with Java Generics and decided to rewrite their API. They will now introduce <a href="http://blog.jooq.org/2015/04/01/dont-be-fooled-by-generics-and-backwards-compatibility-use-generic-generic-types/" rel="nofollow" target="_blank">Generic Generic Types</a> to avoid similar problems in the future.<br />
<br />
<br />
The people from ZeroTurnaround had a look at the way we are currently storing information and found lots of 0 bits in the binary representation. As it is really inefficient to keep so many worthless bits, they implemented a new compression algorithm and released it as their new tool: <a href="http://zeroturnaround.com/blog/download-the-internet-in-milliseconds/" rel="nofollow" target="_blank">ZipRebel</a>. The algorithm provides impressive results and allows you to download the whole internet within milliseconds.<br />
<br />
<br />
Do you know the impact of cosmic energy on your Scrum team?<br />
I mean, it's pretty obvious, that changes in the constellation will affect the events happening to a Scrum team. So better read the <a href="http://www.agile42.com/en/blog/2015/04/01/scrum-horrorscopes/" rel="nofollow" target="_blank">Scrum horrorscopes for sprint 7, 2015</a> and adapt accordingly.<br />
<br />
<br />
Do you like PacMan? Than visit <a href="http://maps.google.com/" rel="nofollow" target="_blank">Google Maps</a> and click on the PacMan image at the bottom left to use the streets of a given location for a game.<br />
<br />
But that is not the only April Fool by Google. They also introduced the new website <a href="https://com.google/">https://com.google/</a>. Guess what you see there ;)<br />
<br />
<br />
All of us have been waiting for it and now Microsoft has finally done it: They <a href="http://lumiaconversations.microsoft.com/2015/04/01/microsoft-launches-ms-dos-mobile/" rel="nofollow" target="_blank">launched MS-DOS Mobile</a>! But there is one huge downside, it is especially designed for Lumia smartphones. Lets hope that they adapt it to other devices so that all of us can use it in the future.<br />
<br />
<br />
<h2>
See you next Monday</h2>
for the next (real) issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a> and follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great Java resources during the week.<br />
<br />
Did I miss a great April Fools? Please post the link in the comments.Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-31632908826985779942015-03-30T05:20:00.000+02:002015-03-30T05:22:31.293+02:00Java Weekly 14/15: log4j memory leaks, JBatch monitoring 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMbyxkMsaHc97Q5IqWgf3BUvSLQOdnT6rFl6ywYQcmL9hcY8vy2FN13NQDUzrSfar3dmIQDEiqUKm9SlzECcgonWmNJK6hveZjKHe4v1qjFwP_W6Fj2m-dbkQoKik-gtodZDAGO_E7Lag9/s1600/Java+Weekly+14-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
Mikhail Vorontsov wrote a nice guide about Java Mission Control. If you like to learn more about this tool and how you can use it to profile your application, have a look at: <a href="http://blog.takipi.com/oracle-java-mission-control-the-ultimate-guide/" rel="nofollow" target="_blank">Oracle Java Mission Control: The Ultimate Guide</a>.<br />
<br />
<br />
Do you use log4j in your applications?<br />
If yes, you should have a look at this short article by Ivan Korhner. He and his colleagues found a <a href="http://korhner.github.io/java/debugging/log4j-dynamic-log-leak/" rel="nofollow" target="_blank">memory leak caused by the dynamic creation of log4j loggers</a>. So be careful about creating a new logger for every user.<br />
<br />
<br />
Vladimir Šor wrote an interesting post about lock contention issues when using the java.util.SecureRandom class. This class should be used to generate cryptographically secure random numbers. Doesn't sound like you and me would need that on a daily basis, does it?<br />
Well, as Vladimir explains in his article, lots of libraries use this class internally. The java.io.File class is one of them. You can read more about this issue in: <a href="https://plumbr.eu/blog/shooting-yourself-in-the-foot-with-random-number-generators" rel="nofollow" target="_blank">Shooting yourself in the foot with Random number generators</a>.<br />
<br />
<br />
<h2>
Java EE</h2>
Romain Manni-Bucau wrote a short blog post on how to monitor the execution of a JBatch job with a batchlet: <a href="https://rmannibucau.wordpress.com/2015/03/26/jbatch-report-about-a-previous-step/" rel="nofollow" target="_blank">JBatch: report about a previous step!</a><br />
<br />
<br />
<h2>
This and that</h2>
Daniel Bryant wrote a summary about the session "Recipes for Deploying Java EE Apps with Docker and Kubernetes" given by Arun Gupta at the JavaLand conference. This article gives a nice overview about the session and contains lots of links to other useful resources on this topic: <a href="http://www.infoq.com/news/2015/03/gupta-javaee-docker-kubernetes" rel="nofollow" target="_blank">Arun Gupta: Recipes for Deploying Java EE Apps with Docker and Kubernetes</a>.<br />
<br />
<br />
<h2>
Recordings</h2>
Last week, the NightHacking team visited the JavaLand conference and recorded a vJUG session on Adopt-a-JSR, a vJUG session on Adopt OpenJDK and a set of interviews. You can find all of them on the <a href="http://nighthacking.com/category/javaland-2015/" rel="nofollow" target="_blank">NightHacking website</a>.<br />
<br />
<br />
<h2>
Upcoming online events</h2>
<div>
Markus Eisele will present his session <a href="http://www.meetup.com/virtualJUG/events/221218531/" rel="nofollow" target="_blank">Architecting Large Enterprise Java Projects</a> on Wednesday 1st at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+April+1st+2015" rel="nofollow" target="_blank">5pm UTC</a> at the virtual JUG.</div>
<div>
<div style="margin: 0px;">
<br />
<br /></div>
</div>
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-74488408161675132372015-03-29T22:21:00.001+02:002015-03-29T22:21:51.371+02:00My Trip to JavaLand 2015<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.javaland.eu/1/javaland-2015/" rel="nofollow" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;" target="_blank"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimuZkKLDrnVp1pr3qXnFUawZAK-gSxhUkxvOkWk7T9gMB4iay9mK2S5t3fFntJCEdzFy8Y86qGMspt8ZL9lTJ-hww7xB4Jg9i5M5UU_cLiIU1M2acHA9gxW7Uk7KeHURhUfEahkIB4l_sa/s1600/csm_Logo_JavaLand_63d01a4e88.jpg" /></a></div>
Last week, I attended the <a href="http://www.javaland.eu/1/javaland-2015/" rel="nofollow" target="_blank">JavaLand 2015</a> conference in Brühl, Germany. If you follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, you have probably seen me tweeting about it. And what shall I say, it was a great conference! Well organized, lots of interesting sessions, great discussions and everything happening in a theme park. Yes, that's right: it took place in the <a href="http://www.phantasialand.de/en/home" rel="nofollow" target="_blank">Phantasialand</a> theme park!<br />
<br />
And did I already mention that I hosted a Java quiz as one of the community activities? :-D<br />
<a name='more'></a><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
OK, lets start at the beginning. The <a href="http://www.javaland.eu/1/javaland-2015/" rel="nofollow" target="_blank">JavaLand 2015</a> conference took place at the 24th and 25th of March in <a href="http://www.phantasialand.de/en/home" rel="nofollow" target="_blank">Phantasialand</a> in Brühl and was organized by DOAG and the heise publishing house. The iJUG, the umbrella organization of the german JUGs, acted as a community partner and hosted several community activities. My Java quiz was one of these activities. But more about that later...<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDbTuseGURyqnklXJA3ZxO-dVlbKwt_fXxEVYzKbM-UTUMOEi3WgOMR1H8PLZ4_63mLWNxq3ksJqp71Of76Gj86Kb38pfZ4mFsEkoTeuWF11_NW39ZG6yCNJPZWgC330qzvbjYj4sLlyfM/s1600/WP_20150324_005.jpg" height="240" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;" width="320" /></div>
<h2>
Organization</h2>
Both conference days were jam-packed with a total of <a href="https://www.doag.org/konferenz/konferenzplaner/konferenzplaner.php?locS=1&id=483801" rel="nofollow" target="_blank">105 sessions and 2 keynotes</a>. These were organized in 7 parallel tracks with 8 sessions on the first and 7 sessions on the second day. There was so much going on in parallel that it was often hard to decide for one session.<br />
The only downside was the missing lunch break on both days. Don't get me wrong, the restaurant was open and offered a tasty buffet for more than two hours. But you had to decide which of the two talks, that took place during that time, you want to skip. The advantage on the other hand was that not everyone rushed to the lunch break at the same time and that everyone could at least decide which sessions he/she wants to miss. I am still not sure, if I see this schedule as an up- or downside but I really wanted to attend both talks on the first day.<br />
<br />
<br />
<h2>
Sessions</h2>
I attended lots of great sessions during these two days but I can't describe everything here. So I will try to keep it short and name only a few highlights:<br />
<ul>
<li>Mark Struberg gave an interesting talk about JBatch and Apache BatchEE. He started with an introduction to JBatch, gave some tips based on experience from real projects and ended the session with some special features provided by BatchEE. Especially the strongly typed API and the command line capabilities look great. You can expect to read more about it on this blog in the future.</li>
<li>Lukas Eder gave a great talk about SQL and showcased some advanced language features to answer complex questions within the database instead of the Java code. JPA is an easy and efficient way to implement CRUD use cases but if it gets really complex, SQL is often the better choice.</li>
<li>Jason Porter started the second day with a session about CDI Extensions. This is a topic I want to write about since I joined the CDI 2.0 expert group and even before that. By writing an extension, you can extend the container with your own custom logic and implement your own framework. I will provide more details on this in the near future.</li>
<li>As you might have read during the last weeks, Java EE 8 contains a new Java EE Security JSR. David Blevins and some of his expert group colleagues used the session at the JavaLand to talk about existing ideas and to ask for additional changes that are required by the community. The open way of communication created a very special atmosphere and was the beginning of some interesting discussions after the end of the session.</li>
</ul>
<div>
<br />
<br /></div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHepf7jje1zLeiFOFaZwmGeqRXXl1fI4LcHvEgP_icMnLDPTQ0SaN6VVrjIyHOdyTUplNGlir4zBS9DMus-MnGZhSJ9-ZiwDpcoyvbDM10GOqonVksuLAm8XPtBKjlyvTeC3PcOYsQtuGc/s1600/WP_20150324_001.jpg" height="240" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;" width="320" /></div>
<h2>
Community Activities</h2>
As I already wrote, the iJUG organized several <a href="http://www.javaland.eu/1/program/practicing-the-community-activities/" rel="nofollow" target="_blank">community activities</a>, like a coding dojo, a hackergarten, workshops, an early adopters area, <a href="http://nighthacking.com/category/javaland-2015/" rel="nofollow" target="_blank">NightHacking interviews</a> and much more. I had the honor to be part of it and to host a Java quiz, called J-Pardy, at the first evening. It was really fun (at least after I knew that my setup worked and I started to calm down ;-) ).<br />
<br /></div>
<div>
A similar quiz was hosted last year and I could reuse the hard- and software. A special thanks to Tobias Frech! That made it a lot easier for me. I just implemented some minor enhancements to the soft- and hardware and prepared the questions for several games.<br />
<br /></div>
<div>
There was a professional photographer who took pictures of the initial setup and the games. I hope I can get some of the photos and show them later. The picture above shows the stage at the Wintergarten before we added a few tables for the players.</div>
<div>
<br />
<br />
<h2>
Discussions</h2>
</div>
<div>
Several interesting discussions about existing and upcoming Java EE specifications took place between the sessions and community activities. As a result of it, Mark Struberg, Jason Porter and I pushed a proposal to the Java EE Security JSR github repository. We will see, what the expert group will make of it.<br />
<br />
And we had a discussion about class loader isolation in EAR files and the effect on CDI extensions with all the people in the following tweet (I am the partly hidden guy at the bottom left). This input will hopefully be used in the Java EE 8 specification process.</div>
<blockquote class="twitter-tweet" lang="de">
CDI lovefest with <a href="https://twitter.com/lightguardjp">@lightguardjp</a> <a href="https://twitter.com/holly_cummins">@holly_cummins</a> <a href="https://twitter.com/thjanssen123">@thjanssen123</a> <a href="https://twitter.com/chkal">@chkal</a> <a href="https://twitter.com/dblevins">@dblevins</a> <a href="https://twitter.com/brunoborges">@brunoborges</a> <a href="https://twitter.com/struberg">@struberg</a> <a href="https://twitter.com/hashtag/JavaEE?src=hash">#JavaEE</a> <a href="http://t.co/vkDWFmvkKO">pic.twitter.com/vkDWFmvkKO</a><br />
— Arun Gupta (@arungupta) <a href="https://twitter.com/arungupta/status/580751286041239553">25. März 2015</a></blockquote>
<script async="" charset="utf-8" src="//platform.twitter.com/widgets.js"></script>
<br />
<div>
OK, that's enough for this post. To sum it up: <a href="http://www.javaland.eu/1/javaland-2015/" rel="nofollow" target="_blank">JavaLand 2015</a> was great and I am already looking forward to the next.</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-44714375313397766252015-03-23T07:00:00.000+01:002015-03-23T07:00:08.344+01:00Java Weekly 13/15: JCache, RESTful conversations, Java EE Management 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk3eDDH146cT81HPIWxsGX0UJa1icxFy_kcsmWTZGL9Gk4tu1zjoSvpUS2PGC9lHDvuC2q1N9keK6ZCBET7jsKz5W5SCyACb7pAmfIWt6aRY9oOKHVcGtXAi5nmW5zAjwtRk9OiUrYWOAF/s1600/Java+Weekly+13-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java EE</h2>
<div>
Steve Millidge wrote a nice post about <a href="http://www.payara.co.uk/using_the_jcache_api_with_cdi_on_payara" rel="nofollow" target="_blank">using the JCache API with CDI</a> on the Payara application server. All you need to do is activate the included Hazelcast Data Grid and add one CDI annotation to your method.<br />
<br />
<br />
JPA 2.1 standardized the database schema generation. Roberto Cortez describes in his blog post <a href="http://www.radcortez.com/jpa-database-schema-generation/" rel="nofollow" target="_blank">JPA DATABASE SCHEMA GENERATION</a> the new configuration parameters and shows how these can be used to setup the database during application deployment.<br />
<br />
<div align="center">
<b>See also:</b> <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">JPA 2.1 Overview - 12 features every developer should know</a>.</div>
<br />
<br />
JDBC batching can provide huge performance benefits, if your application performs lots of write operations. But how to activate it, if you are using Hibernate? And does it still improve the performance, if Hibernate takes care of the database communication?<br />
Yes, if Hibernate is configured correctly, JDBC batching improves the performance a lot. Vlad Mihalcea shows in his recent blog post how to use JDBC batching with Hibernate and how to get the best performance out of it: <a href="http://vladmihalcea.com/2015/03/18/how-to-batch-insert-and-update-statements-with-hibernate/" rel="nofollow" target="_blank">How to batch INSERT and UPDATE statements with Hibernate</a>.<br />
<br />
<br />
<h2>
Java EE 8</h2>
Reza Rahman wrote a short introduction to the Java EE Management API 2.0 (JSR 373), one of the most recent additions to the Java EE 8 JSRs. If you like to learn more about the goals of this JSR and how you can participate, have a look at: <a href="https://blogs.oracle.com/theaquarium/entry/java_ee_management_api_2" rel="nofollow" target="_blank">Java EE Management API 2.0 (JSR 373) Spins Up</a>.<br />
<br />
<br />
<h2>
Java 9</h2>
Alex Blewitt wrote an article about Java 9 and possible future changes based on the EclipseCon keynote given by Marc Reinhold. As you probably already know, the Java Module System will be one of the main changes of Java 9. And therefore it is also the main topic of this article. Alex gives an overview about the module structure and the impacts on the existing Java ecosystem. He also has a look at possible changes that might be introduced after Java 9: <a href="http://www.infoq.com/news/2015/03/java9-and-beyond" rel="nofollow" target="_blank">Mark Reinhold on Java 9 and Beyond</a>.<br />
<br />
<br />
<h2>
This and that</h2>
</div>
Gregor Hohpe wrote an in-depth article about <a href="http://www.eaipatterns.com/ramblings/81_restconversation.html" rel="nofollow" target="_blank">RESTful conversations</a>. As he points out in his article, conversation patterns are still relevant in the current era of RESTful applications.<br />
<br />
<br />
<h2>
Recordings</h2>
Last week, Bert Ertman presented a session about <a href="http://zeroturnaround.com/rebellabs/building-modular-java-apps-in-the-cloud-by-bert-ertman/" rel="nofollow" target="_blank">building modular Java applications in the cloud</a> at the vJUG. If you missed it, you can find the replay, a short write-up and an interview with Bert on the Rebellabs blog.<br />
<br />
<br />
<div>
<h2>
Upcoming online events</h2>
</div>
<div>
<div style="margin: 0px;">
This week, the vJUG will present two session from the <a href="http://http%3B//www.javaland.eu" rel="nofollow" target="_blank">JavaLand</a> conference:<br />
<br />
On Tuesday 24th at <a href="http://www.wolframalpha.com/input/?i=14%3A45+UTC+March+24th+2015" rel="nofollow" target="_blank">2:45pm</a> Mani Sarkar & Daniel Bryant will explain <a href="http://www.meetup.com/virtualJUG/events/221243201/" rel="nofollow" target="_blank">how is Java/JVM built</a>.<br />
<br />
On Wednesday 25th at <a href="http://www.wolframalpha.com/input/?i=14%3A45+UTC+March+25th+2015" rel="nofollow" target="_blank">2:45pm</a> Heather VanCura, Andres Almiray, Anatole Tresch and Ed Burns will talk about the Adopt-a-JSR program and how the JSRs could benefit from participation: <a href="http://www.meetup.com/virtualJUG/events/221243069/" rel="nofollow" target="_blank">What's coming in Java.Next?</a><br />
<br />
<br />
<h2>
JavaLand</h2>
I will attend the <a href="http://www.javaland.eu/javaland-2015/" rel="nofollow" target="_blank">JavaLand</a> conference this week and will organize the J-Pardy quiz as part of the <a href="http://www.javaland.eu/themen/startseite/programm/uebe-community-aktivitaeten/" rel="nofollow" target="_blank">community activities</a> on Tuesday evening. If you like to meet, catch me there or write me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>.<br />
<br />
<br /></div>
</div>
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-73240529128688751652015-03-16T05:52:00.000+01:002015-03-16T05:52:19.688+01:00Java Weekly 12/15: CDI, templating in MVC, Keycloak, recorded sessions 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8dmJoJXQioOKT_y8piXfDmqS7xEmZlaed4SdUZImi73nIm0WctteMSlgz444ud_GBzbJRmqUp9kk574-GYZn3RjQRMDkybnzJYzYPVD3vsMj15CsWBy_Mq0ewIVaiGRrph8XP0j0iE8JT/s1600/Java+Weekly+12-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java EE</h2>
<div>
Romain Manni-Bucau explains in his recent blog post how to use CDI event observers to implement the behavior of @Startup without EJB: <a href="https://rmannibucau.wordpress.com/2015/03/10/cdi-and-startup/" rel="nofollow" target="_blank">CDI and @Startup: SOLVED!</a><br />
<br />
<br />
Another useful CDI example was posted by Chris Ritchie. He shows how to use CDI producer methods to read properties from a <i>.properties</i> file and inject them into your beans: <a href="http://blog.chris-ritchie.com/2015/03/inject-external-properties-cdi-java-wildfly.html" rel="nofollow" target="_blank">Inject external properties using CDI, Java and WildFly</a>.<br />
<br />
<br />
Vlad Mihalcea had a detailed look at the CascadeType.LOCK and how the lock is propagated from the parent to the child entities: <a href="http://vladmihalcea.com/2015/03/12/hibernate-cascadetype-lock-gotchas/" rel="nofollow" target="_blank">Hibernate CascadeType.LOCK gotchas</a>.<br />
<br />
<br />
Slow database queries are often the cause for performance issues. Therefore a check for slow queries should always be the first step in the analytic process. How to configure Hibernate statistics to get the required information is described in: <a href="http://www.thoughts-on-java.org/2015/03/how-to-activate-hibernate-statistics-to.html" target="_blank">How to activate Hibernate Statistics to analyze performance issues</a>.<br />
<br />
<br />
<h2>
Java EE 8</h2>
There are already lots of great templating solutions available. Therefore the MVC JSR will not provide an additional one but provide a way to use the existing ones: <a href="https://blogs.oracle.com/theaquarium/entry/templating_in_the_mvc_jsr" rel="nofollow" target="_blank">Templating in the MVC JSR</a>. Ozark, the MVC reference implementation, already supports a list of different templating solutions.<br />
<br />
<br />
It took some time, but now the JSR 375 (Java EE Security API) Expert Group is formed. David Delabassee gives a short overview about the EG members and how to follow the discussions: <a href="https://blogs.oracle.com/theaquarium/entry/jsr_375_update" rel="nofollow" target="_blank">Java EE Security API (JSR 375) Update</a>.<br />
The will be also two upcoming talks about the JSR at JavaLand and Devoxx France.<br />
<br />
<br />
<h2>
Security</h2>
Red Hat has decided to stop their work on the PicketLink project and merge it into Keycloak. You can read more about the reasons and the official announcement: <a href="http://blog.keycloak.org/2015/03/picketlink-and-keycloak-projects-are.html" rel="nofollow" target="_blank">PicketLink and Keycloak projects are merging!</a><br />
<br />
<br />
If you want to give Keycload a try, you should have a look at <a href="http://sebplorenz.blogspot.de/2015/03/setting-up-keycloak-on-wildfly.html" rel="nofollow" target="_blank">Setting up Keycloak on Wildfly</a>. Sebastian Lorenz walks you through the setup process and provides a small sample application to test the configuration.<br />
<br />
<br />
<h2>
This and that</h2>
</div>
Petri Kainulainen wrote a new post in his Getting Started with Gradle series. This time he explains <a href="http://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-web-application-project/" rel="nofollow" target="_blank">how to use gradle to create a web application project</a>.<br />
<br />
<br />
<h2>
Recordings</h2>
Last week, two interesting online events took place. And the good thing about online events is, you can watch the replay, if you missed the live event.<br />
<br />
<br />
Aleksey Shipilëv spoke about the Java Memory Model at the virtual JUG. Here you can find the replay, a short summary of the talk and an interview with Aleksey: <a href="http://zeroturnaround.com/rebellabs/java-memory-model-pragmatics-by-aleksey-shipilev/" rel="nofollow" target="_blank">Java Memory Model Pragmatics by Aleksey Shipilëv</a>.<br />
<br />
<br />
Ales Justin gave an introduction to the CapeDwarf project, an open source implementation of the Google App Engine. If you like to learn more, you can watch the replay here: <a href="https://www.youtube.com/watch?v=epYpOWyf7uM" rel="nofollow" target="_blank">Implementing your own Google App Engine</a>.<br />
<br />
<br />
<div>
<h2>
Upcoming online events</h2>
</div>
<div>
<div style="margin: 0px;">
On Wednesday 18th at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+March+18th+2015" rel="nofollow" target="_blank">5pm UTC</a>, Bert Ertman will talk about <a href="http://www.meetup.com/virtualJUG/events/220599432/" rel="nofollow" target="_blank">Building Modular Java Applications in the Cloud Age</a> at the vJUG.<br />
<br />
<br /></div>
</div>
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-44325100110442551342015-03-10T05:25:00.000+01:002015-04-19T06:17:42.768+02:00How to activate Hibernate Statistics to analyze performance issuesDid you ever ask yourself why a server request took ages on the production system while your local test system was just fine?<br />
Well, we all had these situations and we will have several more of them in the future. In my experience, strange performance drops are often related to slow database queries. But which query causes the problem? And how to find it, if you can't or don't want to activate logging on your database?<br />
<br />
The good news is, there is an easy way to do it. Hibernate can be configured to collect statistics and to provide the required information to analyze the database queries. So lets see what we need to do.<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBcewJfiOh0TN0xlyyNx1pVO6Wn2CEulWbxM4Rp4-nZCDL3PiE1CyJfQI1ezUwth1odbvZ5UZG1bDz6ZCrNp1AgfSXmbOFPcEnie2uZiKD_4nY_rBAxt920beRLFxFVu1UiU19Vlq6JNaM/s1600/Activate+Hibernate+Statistics.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Demo application</h2>
Let's begin with a simple demo application. I know your production code is way more complex, but we only need a few database requests for our statistics. Therefore we will use a Product entity with an ID, a version and a name.<br />
<br/>
<script src="https://gist.github.com/thjanssen/180eb6b5ce5a5794059b.js?file=Product.java"></script>
<br />
We use this in a loop to store 10 records in the database and query them afterwards.<br />
<br/>
<script src="https://gist.github.com/thjanssen/180eb6b5ce5a5794059b.js?file=loop.java"></script>
<br />
<br />
<h2>
Configuration</h2>
<div>
The first thing we need to do is tell Hibernate to generate statistics for us. Therefore we need to set the system property <i>hibernate.generate_statistics</i> to <i>true</i>. Statistics are deactivated by default because it has a bad influence on the performance. So only activate them when you need them.<br />
<br />
Now Hibernate will write one multi-line log statement with summarized statistical information at the end of the session. This is quite useful to get an overview on how much time was spend on database communication. But in most cases we need more information. We need to know, if there was a specific query that took a long time or if we have an overall performance issue. Therefore we would like to log the execution time for each query. This can be done by setting the log level for <i>org.hibernate.stat</i> to <i>DEBUG</i>.<br />
<br />
I used a Wildfly 8.1.0.Final application server with Hibernate 4.3.5 for this example and did the described configuration changes by adding the following lines to the <i>standalone.xml</i> file.<br />
</div>
<br/>
<script src="https://gist.github.com/thjanssen/180eb6b5ce5a5794059b.js?file=standalone.xml"></script>
<h2>
Log output</h2>
<div>
If we execute the test code to store 10 <i>Product</i> entities in the database and read them from the database afterwards, Hibernate writes the following log messages:</div>
<br/>
<script src="https://gist.github.com/thjanssen/180eb6b5ce5a5794059b.js?file=server.log"></script>
As you can see, the specific statistics for the select statement and the overall session statistics were written to the log file.<br />
<br />
The statistics of the select statement show the number or returned rows (10) and the execution time of the statement (0ms). This would be the place where we would identify a slow statement. But there is obviously no performance issue with this simple query ;-)<br />
<br />
The overall statistics provide information about the number of used JDBC connections and statements, the cache usage and the performed flushes. Here you should always check the number of statements first. Lots of performance issues are caused by n+1 select issues that can result in lots of additional queries. You can find more information about how to fix these kind of issues here: <a href="http://www.thoughts-on-java.org/2014/12/5-ways-to-initialize-lazy-relations.html" target="_blank">5 ways to initialize lazy relations and when to use them</a>.<br />
<br />
If you wonder why Hibernate used 21 instead of 11 JDBC statements to store 10 Product entities and read all of them afterwards, you need to set <i>org.hibernate.SQL</i> to <i>DEBUG</i> to get more information. Hibernate required 2 statements to store each new product in the database because it selected the ID of each entity from a database sequence first and used it to store a fully initialized entity in the database.<br />
<br />
<h2>
Conclusion</h2>
If you need to analyze performance issues, Hibernate can collect and log useful statistics about each database query and the whole session. This needs to be activated by setting the system property <i>hibernate.generate_statistics</i> to <i>true</i> and the log level for <i>org.hibernate.stat</i> to <i>DEBUG</i>.<br />
<br />
You can then decide, if the performance issue is caused by the database communication or by some other part of the system. If it is caused by the database communication, you can see if you need to fix it in your Java code or if you need to contact your database administrator to get more detailed information about your slow queries.<br />
<br />
You enjoyed this article and like to learn more about Java EE? Make sure to subscribe below or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com2tag:blogger.com,1999:blog-5355106262763766945.post-86826402997966896402015-03-09T06:00:00.000+01:002015-03-09T06:00:03.884+01:00Java Weekly 11/15: Java Money, REST API evolution, CDI 2.0 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMJulTex7ySiZ_OczRvt0Qp3QTLMnkX84ZMfTjFSZ6wa8f5_M9uyRgoHuQu3HVvNU_PuULpLqXDyLUKkDihNdJC4vpqh1l9iLiClljm-3FatxNoVcxCo_7j9izmGMXxjfufVDYFBPzdbjC/s1600/Java+Weekly+11-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
JSR 354 (Java Money & Currency) was created to ease the work with monetary values in Java. The work is nearly done and it will be released in the near future. Anatole Tresch, the specification lead, described the reasons for this specification and explained several of its features in great detail in: <a href="http://www.infoq.com/articles/JSR-354-Java-Money-Currency-API" rel="nofollow" target="_blank">Go for the Money! JSR 354 Adds First Class Money and Currency Support to Java</a>.<br />
<br />
<br />
A few days ago, Java 8u40 was released and brought a bunch of small improvements, like a Nashorn Class Filter and improvements to the memory management. You can read more about it in the release announcement by Yolande Poirier on the oracle blog: <a href="https://blogs.oracle.com/java/entry/announcing_jdk_8_update_40" rel="nofollow" target="_blank">Announcing Java SE 8 Update 40</a>.<br />
<br />
<br />
<h2>
Java EE</h2>
<div>
Dropwizard and Spring Boot are two very popular frameworks for Java applications that are often used to implement microservices. But which should we choose, if we start with a new application?<br />
Alex Zhitnitsky had a look at both frameworks and created a nice <a href="http://blog.takipi.com/java-bootstrap-dropwizard-vs-spring-boot" rel="nofollow" target="_blank">side by side comparison</a> of them.<br />
<br />
<br />
Romain Manni-Bucau had a look at the usage of CDI Instance and identified <a href="https://rmannibucau.wordpress.com/2015/03/02/cdi-and-instance-3-pitfalls-you-need-to-know/" rel="nofollow" target="_blank">3 pitfalls you should be aware of</a>.<br />
<br />
<br />
In most projects APIs change over time. It is always something developers like to avoid but it happens anyways. The application changes and so does the API.<br />
Décio Sousa explains in his post <a href="http://www.radcortez.com/rest-api-evolution/" rel="nofollow" target="_blank">REST API EVOLUTION</a> how to implement API changes while maintaining retro-compatibility for older clients.<br />
<br />
<br />
<h2>
Java EE 8</h2>
We have been working on the CDI 2.0 specification for several month now and it is time to give an overview about the current status of the work and what is planned for the final release. You can read more about it in <a href="http://www.cdi-spec.org/news/2015/02/23/CDI-2_0_-_A_glimpse_at_the_future/" rel="nofollow" target="_blank">CDI 2.0 - A glimpse at the future</a> on the CDI blog.<br />
<br />
<br />
<h2>
OpenJDK</h2>
Ivan St. Ivanovs completed his series about the support of primitives in generics as part of project Valhalla. In this 3rd part of the series, he had a look at the challenges that this change will bring to the existing APIs and how to cope with them: <a href="http://nosoftskills.com/2015/03/primitives-in-generics-part-3/" rel="nofollow" target="_blank">Primitives in Generics, part 3</a>.<br />
<br />
<br />
<h2>
Upcoming online events</h2>
</div>
<div>
<div style="margin: 0px;">
Ales Justin will give a talk about <a href="http://www.meetup.com/JBoss-User-Group-Worldwide/events/220482287/" rel="nofollow" target="_blank">Implementing your own Google App Engine</a> with JBoss Cape Dwarf at <a href="http://www.wolframalpha.com/input/?i=18%3A00+UTC+Tuesday+March+10th+2015" rel="nofollow" target="_blank">6pm UTC</a> on Tuesday 10th at the vJBUG.</div>
<div>
<div style="margin: 0px;">
<br />
<br /></div>
</div>
<div>
<div style="margin: 0px;">
On Wednesday 11th at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Tuesday+March+11th+2015" rel="nofollow" target="_blank">5pm UTC</a>, Aleksey Shipilëv will talk about <a href="http://www.meetup.com/virtualJUG/events/220599344/" rel="nofollow" target="_blank">Java Memory Model Pragmatics</a> at the vJUG.</div>
</div>
</div>
<br />
<br />
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-25094474411374521712015-03-02T05:10:00.000+01:002015-03-02T05:11:16.670+01:00Java Weekly 10/15: Java Threads, lock modes, JAX-RS caching 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.<br />
<br />
I am using the <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly series</a> 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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLkIswOdKxwvSSGn1qsFqMmZsFj_HqUrBWwjnTdE1ss-uFT7NQks9zX5cNXMsSLUJXLeFPw4Vtw7ko4qFQg2RhjRT_qribHea0EJPhLbhU_fmxfP2XZmf5sB-V2bMrjl45KlCbNFjCJMQt/s1600/Java+Weekly+10-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<br />
<h2>
Java</h2>
Heinz Kabutz published the <a href="http://www.javaspecialists.eu/archive/Issue226.html" rel="nofollow" target="_blank">226th issue of his Java Specialists' Newsletter</a>. This time he has a look at the creation of threads in Java and the role of the SecurityManager in this process. As always, a very interesting read for all advanced Java developers!<br />
<br />
<br />
<h2>
Java EE</h2>
<div>
Have you heard about Unsynchronized Persistence Contexts in JPA 2.1?<br />
Well, don't worry, if you haven't. It is rarely discussed in the community. But Reza Rahman found a short video by the JPA 2.1 specification lead Linda DeMichiel about it: <a href="https://blogs.oracle.com/theaquarium/entry/unsynchronized_persistence_contexts_in_jpa" rel="nofollow" target="_blank">Unsynchronized Persistence Contexts in JPA 2.1/Java EE 7</a>.<br />
<br />
<div align="center">
<b>See also:</b> <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">JPA 2.1 Overview - 12 features every developer should know</a>.</div>
<br />
<br />
Vlad Mihalcea published the next issue of his Hibernate Master Class series. This time he shows <a href="http://vladmihalcea.com/2015/02/24/hibernate-locking-patterns-how-do-pessimistic_read-and-pessimistic_write-work/" rel="nofollow" target="_blank">how the PESSIMISTIC_READ and PESSIMISTIC_WRITE lock modes work</a>.<br />
<br />
<br />
Abhishek Gupta wrote a nice post about cache-control header in JAX-RS and how this can be simplified by using CDI producer methods to create an initialized CacheControl class. Always nice to see how these simple producer methods can provide huge benefits: <a href="https://abhirockzz.wordpress.com/2015/02/20/simplifying-jax-rs-caching-with-cdi/" rel="nofollow" target="_blank">Simplifying JAX-RS caching with CDI</a>.<br />
<br />
<br />
<h2>
Java EE 8</h2>
Weld is the CDI reference implementation and the development team is currently working on a series of CDI 2.0 prototype releases. The latest release is Weld 3.0.0.Alpha5 and was published a few days ago. Jozef Hartinger wrote a blog post to announce the release and describe its highlights: <a href="http://weld.cdi-spec.org/news/2015/02/25/weld-300Alpha5/" rel="nofollow" target="_blank">Weld 3.0.0.Alpha5</a>.<br />
<br />
<br />
<h2>
This and that</h2>
</div>
<div>
When we test a method, we often want to call it with different sets of parameters and make sure that we get the correct result. This can be done in lots of different ways. One of the better ways is to use <a href="http://blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html#.VO2_nMTMcyQ.twitter" rel="nofollow" target="_blank">parameterized JUnit tests</a> as Rafał Borowiec describes in his blog post.<br />
<br />
<br />
Are you familiar with branching in git or just like to refresh your knowledge? Than have a look at <a href="https://pcottle.github.io/learnGitBranching/" rel="nofollow" target="_blank">Learn Git Branchning</a>. This site provides an interactive tutorial to explain the concepts behind branching in git.</div>
<div>
</div>
<br />
<br />
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
for the next issue of <a href="http://www.thoughts-on-java.org/p/java-weekly.html" target="_blank">Java Weekly</a>. You don't want to wait so long, than follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a>, where I share great resources during the week.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-84205538022559033082015-02-23T05:28:00.000+01:002015-02-23T05:51:41.852+01:00Java Weekly 9/15: Reflection, a Java 8 pitfall, Flyway, MVC 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.<br />
<br />
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.<br />
<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJH-Ev6l1NGsBgemVhpxHJBL05Ks2dm1dk9KGJ4YiEXIZz3scaWp991EB1msQD1rIJeI23Ws-BKocYqlBSr5SoWMOOZvMT61ZyDqcfB_mRAhp89yaKd-NDvFFyeBFZn5Kp0PY1RlTd_hMr/s1600/Java+Weekly+9-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<br />
<h2>
Java</h2>
Do you like to learn more about reflection in Java?<br />
There was a great vJUG session with Heinz Kabutz about it. Here you can find the recorded session, a write up about it and a short interview with Heinz at the RebelLabs blog: <a href="http://zeroturnaround.com/rebellabs/vjug-write-up-live-reflection-madness/" rel="nofollow" target="_blank">vJUG Write Up: Live Reflection Madness</a>.<br />
<br />
<br />
Daniel Shaya discovered an awful pitfall in Java8, after he started to use Files.lines() in Java 8. He explained the issue and how to avoid it in: <a href="http://www.rationaljava.com/2015/02/java-8-pitfall-beware-of-fileslines.html" rel="nofollow" target="_blank">Java 8 pitfall - Beware of Files.lines()</a>.<br />
<br />
<br />
<h2>
Java EE</h2>
<div>
Using JPA is the most common approach in Java EE to persist data to a database. The latest update (JPA 2.1 as part of Java EE 7) introduced several new features to the specification. You can get an overview and a cheat sheet about the new features here: <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" rel="nofollow" target="_blank">JPA 2.1 - 12 features every developer should know</a>.<br />
<br />
<br />
If you read the Java Weekly on a regular basis, you should be well aware of Arun Guptas Hanginar Series. Last week, he did the 6th Hanginar with Axel Fontaine about <a href="http://blog.arungupta.me/database-migrations-in-javaee-flyway-hanginar6/" rel="nofollow" target="_blank">database migration with Flyway</a>. Don't worry if you missed the live event. You can still watch the replay and learn more about Flyway and how it integrates with Java EE applications.<br />
<br />
<br />
Using EARs in Java EE can be an issue, if you want to create portable applications. As Mark Struberg describes in his recent blog post, the Java EE specification does not clearly define isolation and visibility of classes: <a href="https://struberg.wordpress.com/2015/02/18/cdi-in-ears/" rel="nofollow" target="_blank">CDI in EARs</a>.<br />
After he published his post, several discussions started on his blog and twitter about it and were continued on the CDI mailing list: <a href="http://cdi-development-mailing-list.1064426.n5.nabble.com/Extension-handling-in-EARs-td5710991.html;cid=1424602437521-43" rel="nofollow" target="_blank">CDI Mailing List - Extension handling in EARs</a>.<br />
<br />
<br />
<h2>
Java EE 8</h2>
MVC is one of the new specifications that will be part of Java EE 8. Manfred Riem, the co-spec lead of the JSR, started to collect interesting links and resources about it: <a href="https://mvc.zeef.com/manfred.riem" rel="nofollow" target="_blank">MVC</a>.<br />
<br />
<br />
Arjan Tijms collects interesting links about the <a href="https://javaee8.zeef.com/arjan.tijms" rel="nofollow" target="_blank">specifications that will be part of Java EE 8</a> since April 2014. He now analyzed the popularity of the different links and tried to find the <a href="http://arjan-tijms.omnifaces.org/2015/01/the-most-popular-upcoming-java-ee-8.html" rel="nofollow" target="_blank">most popular specification in Java EE 8</a>.<br />
<br />
<br />
<h2>
OpenJDK</h2>
Two weeks ago, Ivan St. Ivanovs first post of his series about <a href="http://nosoftskills.com/2015/02/primitives-in-generics-part-1/" rel="nofollow" target="_blank">using primitives in generics</a> was already part of the Java Weekly. Now he published the second post in which he describes the current proposal implemented in Project Valhalla: <a href="http://nosoftskills.com/2015/02/primitives-in-generics-part-2/" rel="nofollow" target="_blank">Primitives in Generics, part 2</a>.<br />
<div>
<br /></div>
<div>
<br /></div>
<h2>
This and that</h2>
</div>
<div>
The HTTP/2 specification was approved by the IESG. David Delabassee created a short write up with links to some useful resources about it and what it means for use Java developers: <a href="https://blogs.oracle.com/theaquarium/entry/http_2_is_done" rel="nofollow" target="_blank">HTTP/2 is done</a>.</div>
<br />
<div>
</div>
<br />
<h2>
See you next Monday</h2>
<div>
<div style="margin: 0px;">
on <a href="http://thoughts-on-java.org/">thoughts-on-java.org</a> for the next issue of Java Weekly. If you don't want to wait until next Monday, follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> where I share great resources regularly.<br />
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-14874143470779187852015-02-17T05:19:00.000+01:002015-02-17T05:47:22.320+01:00JPA 2.1 - 12 features every developer should know<div>
<span style="line-height: 24px;">If you are a regular reader of this blog, you know that I wrote several articles about features and enhancements introduced with JPA 2.1. One thing that was missing, was a general overview about all the changes. So here it is :-)<br />
The following paragraphs provide a description of the 12 features and enhancements introduced with JPA 2.1. And as a special bonus, I created a cheat sheet with a short description and additional code snippets for each change, which you can <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html#cheatSheet">download for free</a>.</span><br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMEbXPmDZXNkdvWRthx6X1IdI5AHQBJ7Q7uYmJ7LgTXbdPAPhV9Zc_QkHx9c1X-unt2hYk6EDC8I_zEcPZrbUFBllm7A6dnDylC-o2N9NWe4JUcz3_vqZis-xYWz_LUuEOjoJO2K47M30-/s1600/JPA+2.1+-+12+features+every+developer.png" width="100%" />
<span style="line-height: 24px;"></span>
<a name='more'></a></div>
<div>
<h2>
<span style="line-height: 36px;">Features and Enhancements in JPA 2.1</span></h2>
</div>
<div>
<span style="line-height: 24px;"><b>Named Stored Procedure Query</b></span><br />
<div>
<span style="line-height: 24px;">Sometimes it is easier or more efficient to use a stored procedure to perform the operations within the database. Before JPA 2.1 the only way to call a stored procedure was to use a native query. The newly introduced <i>@NamedStoredProcedureQuery</i> can now be used to annotate a query to call the stored procedure.</span><br />
<span style="line-height: 24px;"><br /></span></div>
<div>
<div>
</div>
</div>
</div>
<span style="line-height: 24px;"><b>Stored Procedure Query</b></span><br />
<span style="line-height: 24px;">The Stored Procedure Query is an alternative way to implement a stored procedure call without using annotations. For this purpose the </span><i style="line-height: 24px;">EntityManager</i><span style="line-height: 24px;"> was extended by the </span><i style="line-height: 24px;">createStoredProcedureQuery(String procedureName, Class... resultClasses)</i><span style="line-height: 24px;"> method.</span><br />
<div>
<span style="line-height: 24px;"><br /></span></div>
<span style="line-height: 150%;"><b>Attribute
Converter</b></span><br />
<div>
<span style="line-height: 24px;">Attribute Converter provide a nice and easy way to define a custom mapping between your property on the entity and the database column. The only thing that is needed is a class that implements the <i>AttributeConverter</i> interface and is annotated with <i>@Converter</i>. You can find a more detailed introduction to Attribute Converter in <a href="http://www.thoughts-on-java.org/2013/10/jpa-21-how-to-implement-type-converter.html" target="_blank">JPA 2.1 - How to implement an Attribute Converter</a>.<br />
One of the most obvious ways to use an Attribute Converter is to implement a custom type mapping to persist a not supported data type or to change an existing default mapping, as it was done in <a href="http://www.thoughts-on-java.org/2013/10/jpa-21-type-converter-better-way-to.html" style="line-height: 24px;" target="_blank">JPA 2.1 Attribute Converter - The better way to persist enums</a>. Or you could keep the type and change the stored value to implement some business requirements like encryption: <a href="http://www.thoughts-on-java.org/2014/06/how-to-use-jpa-type-converter-to.html" target="_blank">How to use a JPA Attribute Converter to encrypt your data</a>.</span><br />
<br /></div>
<div style="margin: 0px;">
<span style="line-height: 24px;"><b>Constructor Result Mapping</b></span></div>
<div style="margin: 0px;">
<span style="line-height: 24px;">The <i>@ConstructorResult</i> annotation is a handy addition to the already existing <i>@SqlResultSetMapping</i> and can be used to map the result of a query to a constructor call.</span></div>
<div style="margin: 0px;">
<span style="line-height: 24px;"><br /></span></div>
<div>
<span style="line-height: 24px;"><b>Programmatic Named Queries</b></span></div>
<span style="line-height: 24px;">Before JPA 2.1 the <i>@NamedQuery</i> annotation was the only way to define named queries. A programmatic creation was not supported. This was changed with JPA 2.1. The </span><i style="line-height: 24px;">EntityManager</i><span style="line-height: 24px;"> now provides the </span><i style="line-height: 24px;">addNamedQuery(String name, Query query)</i><span style="line-height: 24px;"> method to do this.</span><span style="line-height: 24px;"></span><br />
<div>
<span style="line-height: 24px;"><br /></span></div>
<span style="line-height: 24x;"><b>Named Entity Graph</b></span><br />
<div>
<div>
<div>
<span style="line-height: 24px;">Lazy loading of relations between entities is a common pattern to load only the required information from the database and to improve the performance of the application. While this is a great feature as long as the related entities are not required, it creates additional load when the relations need to be initialized. There are multiple ways to <a href="http://www.thoughts-on-java.org/2014/12/5-ways-to-initialize-lazy-relations.html" target="_blank">initialize these lazy relations</a> and using Named Entity Graphs is one of the better ones. </span><br />
<span style="line-height: 24px;">The annotations <i>@NamedEntityGraph</i>, <i>@NamedAttributeNode</i> and <i>@NamedSubGraph</i> allow us to define a graph of entities that will be loaded from the database. You can find a more detailed description on how to do this in <a href="http://www.thoughts-on-java.org/2014/03/jpa-21-entity-graph-part-1-named-entity.html" target="_blank">JPA 2.1 Entity Graph - Part 1: Named entity graphs</a>.</span><br />
<br />
<span style="line-height: 24px;"><b>Entity Graph</b></span></div>
<div>
<span style="line-height: 24px;">Entity Graphs are the second option introduced with JPA 2.1 to define a graph of entities that shall be loaded from the database and their usage is similar to Named Entity Graphs. The only difference is that Entity Graphs are defined via a Java API and not via annotations. Therefore the <i>EntityManager</i> was extended by the <i>createEntityGraph(Class<t> rootType)</t></i> method. This is explained in more detail in <a href="http://www.thoughts-on-java.org/2014/04/jpa-21-entity-graph-part-2-define.html" target="_blank">JPA 2.1 Entity Graph - Part 2: Define lazy/eager loading at runtime</a>.</span><br />
<br />
<span style="line-height: 24px;"><b>JPQL Enhancements</b><br />
There were several enhancements to the JPQL which can come in handy. You can now use the keyword <i>ON</i> to define additional join parameters, call database functions by using <i>FUNCTION</i> and downcast entities with <i>TREAT</i>.</span>
<br />
<span style="line-height: 24px;"><br /></span></div>
<span style="line-height: 24px;"><b>Criteria API Bulk Operations</b></span><br />
<div>
</div>
<div>
<span style="line-height: 24px;">Up to JPA 2.1 the Criteria API did not provide any support for update or delete operations. The only options available were to perform the update on an entity or to write a native query to update multiple records at once. As described in <a href="http://www.thoughts-on-java.org/2013/10/criteria-updatedelete-easy-way-to.html" target="_blank">Criteria Update/Delete - The easy way to implement bulk operations with JPA2.1</a>, the Criteria API was extended with <i>CriteriaUpdate</i> and <i>CriteriaDelete</i> to also support bulk write operations.</span><br />
<br /></div>
</div>
<div>
<span style="line-height: 150%;"><b>Unsynchronized
Persistence Context</b></span><br />
<span style="line-height: 24px;">Using a synchronized persistence context to propagate every change to the database is the default approach in JPA. If you need more control about the database propagation, you can now use the unsynchronized persistence context. Therefore you need to provide the synchronization mode to the injection with <i>@PersistenceContext(synchronization=SynchronizationType.UNSYNCHRONIZED). </i>You then need to call </span><span style="line-height: 24px;"><i>EntityManager.joinTransaction() </i>manually t</span><span style="line-height: 24px;">o synchronize the changes.</span></div>
<div>
<br />
<span style="line-height: 150%;"><b>Generating
DB Schema</b></span></div>
<div>
<span style="line-height: 24px;">Up to JPA 2.1 you needed to use vendor specific configuration parameter to define the database setup in the <i>persistence.xml</i> file. Starting from version 2.1 there is also a standard way to do this. Therefore the specification defines the following long list of parameters:</span><br />
<ul>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.database.action</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.scripts.action</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.create-source</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.drop-source</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.create-database-schemas</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.scripts.create-target</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.scripts.drop-target</span></li>
<li><span style="line-height: 24px;">javax.persistence.database-product-name</span></li>
<li><span style="line-height: 24px;">javax.persistence.database-major-version</span></li>
<li><span style="line-height: 24px;">javax.persistence.database-minor-version</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.create-script-source</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.drop-script-source</span></li>
<li><span style="line-height: 24px;">javax.persistence.schema-generation.connection</span></li>
<li><span style="line-height: 24px;">javax.persistence.sql-load-script-source</span></li>
</ul>
<br /></div>
<div>
<span style="line-height: 150%;"><b>CDI-Support
in Entity Listener</b></span></div>
<div>
<span style="line-height: 24px;">The integration with CDI was improved with JPA 2.1. You can now use CDI to inject beans into <i>EntityListener</i>s and to implement the <i>@PreDestroy</i> and <i>@PostConstruct</i> methods.</span>
<br />
<br />
<h2>
To sum it up</h2>
</div>
</div>
<div>
<span style="line-height: 24px;">From my point of view, JPA 2.1 - despite being just a minor release - introduced some great improvements to the specification. I especially like the entity graphs and the stored procedure queries but also the AttributeConverter can be quite handy. Which features do you like best?</span></div>
<div>
<br /></div>
<div>
<span style="line-height: 24px;">Sometimes it is difficult to remember all the implementation details, if you need them. This is where I personally like to have a cheat sheet that provides all the information I need, <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html#cheatSheet">like this one ;-)</a></span></div>Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com3tag:blogger.com,1999:blog-5355106262763766945.post-40769663377156324412015-02-16T05:25:00.000+01:002015-02-16T06:01:03.617+01:00Java Weekly 8/15: Docker, JVM mysteries, a dynamic scripting lib 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.<br />
<br />
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.<br />
<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsT2l1s-jVzkY36I4JfSNwj9RbCiX4-5ozCbNHjTaW8XVC7WSmaAdwe_zi07OcXm6a21G4_hIUZKF-wExj3Uvqjjmux9LCafibNDHn6LxyOD63CpRYWw0XE6cPEZQ8Tyz9rylfhnbqRDRa/s1600/Java+Weekly+8-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<br />
<h2>
Java</h2>
While helping a colleague with a support case, Nikita Salnikov-Tarnovski stumbled upon an interesting problem. The JVM tools reported <a href="https://plumbr.eu/blog/jvm-having-access-to-less-memory-than-xmx" rel="nofollow" target="_blank">less available memory than the specified -Xmx</a> and to make it completely weird, different tools reported different values. He dug deep and found the answer somewhere well hidden in the JVM code.<br />
<br />
<br />
<h2>
Java EE</h2>
<div>
Vlad Mihalcea published the next post of his Hibernate Masterclass. This time, he has a look at the OPTIMISTIC_FORCE_INCREMENT lock mode: <a href="http://vladmihalcea.com/2015/02/09/hibernate-locking-patterns-how-does-optimistic_force_increment-lock-mode-work/" rel="nofollow" target="_blank">HIBERNATE LOCKING PATTERNS – HOW DOES OPTIMISTIC_FORCE_INCREMENT LOCK MODE WORK</a>.<br />
<br />
<br />
Several errors in the Java EE 7 specification were found over the last nearly two years. These will be addressed in the upcoming maintenance release: <a href="https://blogs.oracle.com/theaquarium/entry/java_ee_7_maintenance_release" rel="nofollow" target="_blank">Java EE 7 Maintenance Release</a>.<br />
<br />
<br />
<h2>
Docker</h2>
Last week Arun Gupta gave a great talk about <a href="http://zeroturnaround.com/rebellabs/vjug-write-up-package-your-java-ee-application-using-docker-and-kubernetes/" rel="nofollow" target="_blank">packaging Java EE applications using Docker and Kubernetes</a> in the vJUG. Don't worry, if you missed it. You can watch the replay and a short interview with Arun or read the write up of the session at the RebelLabs blog.<br />
<br />
<br />
Josh Dreyfuss had a look at Docker and the current hype about it. Despite being a great technology, he spotted <a href="http://blog.takipi.com/ignore-the-hype-5-docker-misconceptions-java-developers-should-consider/" rel="nofollow" target="_blank">5 misconceptions a Java developer should consider</a>.<br />
<br /></div>
<div>
<br /></div>
<h2>
This and that</h2>
<div>
Should we write documentation and if yes, how much documentation do we really need? There are different approaches ranging from document everything to write no documentation at all. As so often, the best approach is somewhere in between those extremes.<br />
Petri Kainulainen wrote about his 3 requirements to good documentation and which documents we really need: <a href="http://www.petrikainulainen.net/software-development/processes/writing-just-enough-documentation/" rel="nofollow" target="_blank">Writing Just Enough Documentation</a>.</div>
<div>
<br />
<br />
James Kojo, Vasanth Asokan, George Campbell and Aaron Tull wrote an interesting post on the netflix tech blog about their dynamic scripting library Nicobar which is used to support more than 1000 different devices with billions of requests per day: <a href="http://techblog.netflix.com/2015/02/nicobar-dynamic-scripting-library-for.html" rel="nofollow" target="_blank">Nicobar: Dynamic Scripting Library for Java</a>.</div>
<br />
<div>
</div>
<br />
<h2>
Upcoming online events</h2>
<div>
<div style="margin: 0px;">
Bad news for all vJUG and vJBUG fans: This weeks sessions will run at the same time. So you need to decide which session you want to attend live and which you will watch as a replay:</div>
<div style="margin: 0px;">
<br />
<br /></div>
<div style="margin: 0px;">
Eric D. Schabell will give his talk <a href="http://www.meetup.com/JBoss-User-Group-Worldwide/events/220085003/" rel="nofollow" target="_blank">Hit the ground running with BPM — a starter kit</a> at <a href="http://www.wolframalpha.com/input/?i=18%3A00+UTC+Tuesday+February+17th+2015" rel="nofollow" target="_blank">6pm UTC</a> on Tuesday 17th at the vJBUG.</div>
<div>
<div style="margin: 0px;">
<br />
<br /></div>
</div>
<div>
<div style="margin: 0px;">
At the same time Heinz Kabutz will talk about <a href="http://www.meetup.com/virtualJUG/events/220187850/" rel="nofollow" target="_blank">The Live Reflection Madness</a> at the vJUG.</div>
</div>
<div>
<div style="margin: 0px;">
<br /></div>
</div>
<div>
<div style="margin: 0px;">
<br /></div>
</div>
<h2>
See you next week!</h2>
<div>
<div style="margin: 0px;">
These were the most interesting resources I found this week. I hope you find them useful and I see you next Monday for the next issue. You can subscribe below to get an email notification for it or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-7377322312449305142015-02-09T05:25:00.000+01:002015-02-09T05:25:23.555+01:00Java Weekly 7/15: Clustering WebSockets, Batch API Lab, Valhalla 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.<br />
<br />
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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH3nTZ3FYhxbEMkZaYnO0fRzj45iHbzRB1tyWQfnWuV2y7YC3WcmyslMmHl3T1yX1USn8Ei-lh2pbX5b99z8J4hVxvVgUmdQ06-CnvjLxAYgaEghTk2_CFToXNZes8DzcY4KVIDLyUOahK/s1600/Java+Weekly+7-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<br />
<h2>
Java EE</h2>
<div>
Navin Surtani posted the second part of his series about clustering WebSockets on Wildfly. </div>
<div>
In the first part, he described the overall architecture, how to configure the Wildfly server for Amazons EC2 and had a look at some parts of the server and client code: <a href="http://blog.c2b2.co.uk/2014/12/clustering-websockets-on-wildfly.html" rel="nofollow" target="_blank">Clustering WebSockets on Wildfly</a>.</div>
<div>
The second part describes how to setup and use Infinispan to store the data of the application: <a href="http://blog.c2b2.co.uk/2015/02/clustering-websockets-on-wildfly-part-2.html" rel="nofollow" target="_blank">Clustering WebSockets on WildFly - Part 2</a>.</div>
<div>
<br />
<br />
Arun Gupta presented the next issue of his Hanginar series. This time Reza Rahman gave an interesting presentation about <a href="http://blog.arungupta.me/domain-driven-design-javaee-hanginar5" rel="nofollow" target="_blank">Domain Driven Design using Java EE</a>.<br />
<br /></div>
<div>
<br /></div>
<div>
There was a Hands-on-Lab on the Java Batch API (JSR 352) held at JavaOne2014. It used 5 exercises to go from an introduction to some of the most advanced features. </div>
<div>
I have good news for you, if you could not attend it (like myself). David Delabassee wrote last week, that it is available on github: <a href="https://blogs.oracle.com/theaquarium/entry/batch_api_hands_on_lab" rel="nofollow" target="_blank">Batch API Hands-on Lab</a>.</div>
<div>
<br /></div>
<div>
<br /></div>
<h2>
OpenJDK</h2>
<div>
Project <a href="http://openjdk.java.net/projects/valhalla/" rel="nofollow" target="_blank">Valhalla</a> is an experimental project to develop major new features for Java. One of them is the usage of primitives in generics. Ivan St. Ivanov tried the current version of this feature with the Bulgarian JUG and started an interesting blog series about it: <a href="http://nosoftskills.com/2015/02/primitives-in-generics-part-1/" rel="nofollow" target="_blank">Primitives in Generics, part 1</a>.</div>
<div>
In this first post, he has a look at the current state of generics in Java and explains some of the reasons for not supporting primitives. The next issue will show the current state of the proposal in the Valhalla project. I can't wait to read it :-)</div>
<div>
<br /></div>
<div>
<br /></div>
<h2>
Java EE 8</h2>
<div>
Arjan Tijms posted some insides from the work on the JSF 2.3 specification and described some new features related to the alignment with the CDI specification and an additional lifecycle event: <a href="http://jdevelopment.nl/jsf-23/">What’s new in JSF 2.3?</a></div>
<div>
<br /></div>
<div>
<br /></div>
<h2>
This and that</h2>
<div>
Do you know JBoss Forge? It is a great tool to speedup your development and among other great things, avoid the ridicules copy and paste during project setup. Normally it is used as a plugin for eclipse and intellij or as a command line tool. But as George Gastaldi shows in this screencast, it can also be integrated into NetBeans: <a href="https://www.youtube.com/watch?v=sPVh6-CZWfA" rel="nofollow" target="_blank">JBoss Forge NetBeans integration</a>.</div>
<div>
<br /></div>
<div>
<div>
<div>
<br />
<h2>
Upcoming online events</h2>
</div>
<div>
Arun Gupta will give his talk <a href="http://www.meetup.com/virtualJUG/events/219931825/" rel="nofollow" target="_blank">Package your Java EE application using Docker and Kubernetes</a> at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Thursday+February+12th+20157" rel="nofollow" target="_blank">5pm UTC</a> on Thursday 12th at the vJUG.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
One hour later (<a href="http://www.wolframalpha.com/input/?i=18%3A00+UTC+Thursday+February+12th+20157" rel="nofollow" target="_blank">6pm UTC</a> on Thursday 12th ), Stéphane Épardaud will give his talk <a href="http://www.meetup.com/JBoss-User-Group-Worldwide/events/220111268/" rel="nofollow" target="_blank">Ceylon From Here to Infinity: The Big Picture and What's Coming</a> at the vJBUG.</div>
<div>
<br /></div>
<div>
<br /></div>
<h2>
See you next week!</h2>
</div>
<div>
These were the most interesting resources I found this week. I hope you find them useful and I see you next Monday for the next issue. You can subscribe below to get an email notification for it or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.<br />
<br />
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
</div>
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-86853398898272543022015-02-02T05:11:00.000+01:002015-02-03T20:59:09.726+01:00Java Weekly 6/15: Micro Benchmarking, NoSQL with Hibernate, Horror Stories 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.<br />
<br />
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.<br />
<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqb9UiXgKH3E-Tr9yIurTc7d13phAMaHXKCPpEdx-nCiijpaceCZqiFwT0R1-cx9rY5O4SRz03cOSccEExQSo2yNZuYl3tG0ovb9WaWVdm-XdtrwVo_p2bLLVAWFLRifsR7JBOaS7iAo2C/s1600/Java+Weekly+6-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
<div>
Marius Herring created a nice list of code snippets to show how to <a href="http://www.deadcoderising.com/java-8-no-more-loops/" target="_blank">replace classic loops with the Java 8 stream API</a>. It is always great to see how clean and easy the new code is. The stream API allows us to concentrate on the WHAT and to get rid of (nearly) all of the HOW.<br />
<br />
<br />
Are you familiar with threads in Java? Than you should check your knowledge with Alex Zhitnitskys list of <a href="http://blog.takipi.com/java-thread-magic-tricks/" target="_blank">5 Things You Never Knew You Can Do with Java Threads</a>. And if not, you can use this article as a start.<br />
<br />
<br />
How to tackle performance issues? Measure, don't guess! Antonio Goncalves shows how to use the Java Microbenchmarking Harness (JMH) to create proper performance benchmarks: <a href="http://antoniogoncalves.org/2015/01/15/micro-benchmarking-with-jmh-measure-dont-guess/" target="_blank">Micro Benchmarking with JMH: Measure, don’t guess!</a><br />
<br />
<br />
Lambdas are often a great way to write Java code but they seem to have one drawback: They create lots of objects. Therefore Peter Lawrey tried to find out, if <a href="http://vanillajava.blogspot.com/2015/01/java-lambdas-and-low-latency.html" target="_blank">Lambdas are suitable for low latency applications</a>. </div>
<div>
<br />
<br /></div>
<h2>
Java EE</h2>
<div>
Have you heard about the Hibernate OGM project? It tries to bring JPA and NoSQL databases together. You can learn more about it in the recent hanginar by Arun Gupta and Emmanuel Bernard: <a href="http://blog.arungupta.me/hibernate-ogm-nosql-solutions-javaee-hanginar4/" target="_blank">Hibernate OGM: NoSQL solutions for Java EE</a>.<br />
<br />
<div>
<br />
<h2>
This and that</h2>
Roberto Cortez posted a new issue of his development horror stories. This time with a real release nightmare: <a href="http://www.radcortez.com/development-horror-story-release-nightmare/" target="_blank">Development Horror Story – Release Nightmare</a>.<br />
<br />
<br />
In the past, we did a huge upfront design before we started to implement our applications. Then we recognized that this approach has many drawbacks and called it an anti-pattern. We wanted to be agile and keep everything as simple as possible. As we see now, this might be to extreme. We often need at least some design upfront. Petri Kainulainen gives some advice on how to find the right balance in the design process: <a href="http://www.petrikainulainen.net/software-development/design/we-are-gonna-need-it/" target="_blank">We Are Gonna Need It</a>.<br />
<br />
<br />
Are you missing some annotations to express yourself when looking at your colleagues code? Than you should have a look at the <a href="https://code.google.com/p/gag/" target="_blank">Google Annotations Gallery</a> :-)<br />
<br />
<br /></div>
<h2>
See you next week!</h2>
</div>
<div>
These were the most interesting resources I found this week. I hope you find them useful and I see you next Monday for the next issue. You can subscribe below to get an email notification for it or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.<br />
<br />
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-60200100278939908432015-01-26T05:16:00.000+01:002015-01-26T05:16:19.900+01:00Java Weekly 5/15: CDI in Java SE, DeltaSpike, James Gosling 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.<br />
<br />
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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF_4_6S-NH0xrPSxsZEkJst-VfaHgNK9GAj-MRUCmPXPKMW347aMT-Vrx5jRbW_m8r1nB1yud32LrLE7uW1ypr6nHu9Q5pIl_sZ-uSxCWZOruLn4pYoofu_CUgl5hvIVsu_92HxY7bIxNN/s1600/Java+Weekly+5-15.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
<div>
Java 8 introduced <i>Optional<t></t></i> to provide a more convenient way to work with possible null values. That sounds great! But if you are new to <i>Optional<t></t></i>, you might be wondering about the advantages of using the methods <i>isPresent()</i> and <i>get()</i> instead of the good old null check. And you are right, if you use it that way. But there are multiple other ways to use <i>Optional</i><t> as Dadepo Aderemi shows in his post: <a href="http://geekabyte.blogspot.co.il/2015/01/using-optional-effectively-in-java-8.html" target="_blank">Using Optional Effectively in Java 8</a>.</t></div>
<div>
<br />
<br />
Standardizing the usage of CDI in a Java SE environment is one of the goals of the upcoming CDI 2.0 specification. But it is already possible to use CDI in Java SE. Weld and OpenWebBeans provide proprietary ways to start their container in Java SE and Apache DeltaSpike provides a unified API for it.<br />
Rudy De Busscher describes in his post <a href="http://javaeesquad.blogspot.be/2014/12/cdi-for-java-se-already-standardised.html" target="_blank">CDI for Java SE already standardised with DeltaSpike</a> how to do it and which pitfalls you should avoid.<br />
<br />
<br />
Speaking of DeltaSpike, Antoine Sabot-Durand gave a great presentation about the Apache project this week. And don't worry, if you missed it. You can watch the replay <a href="https://plus.google.com/events/ck80nuastknl83q1j9uiqsq9p9s" target="_blank">here</a> and Antoine uploaded the slides to <a href="http://fr.slideshare.net/antoinesd/apache-deltaspike-the-cdi-toolbox" target="_blank">slideshare</a>.<br />
<br />
<br />
But that was not the only great online event this weak. The vJUG hosted a session with James Gosling who talked about the wave glider project. You can get more information about the project, watch the replay of the session and an interview with James Gosling on the <a href="http://zeroturnaround.com/rebellabs/vjug-write-up-wave-gliders-and-java-with-james-gosling/" target="_blank">RebelLabs site</a>.<br />
<br />
<br />
Heinz Kabutz published <a href="http://www.javaspecialists.eu/archive/Issue225.html" target="_blank">issue 225 of The Java Specialists Newsletter</a>. This time, he shows a nice trick to hide interface methods.<br />
<br />
<br /></div>
<h2>
Java EE</h2>
<div>
Arjan Tijms is working on a very detailed article series about JACC. In the first part of the series, Arjan describes the concept of role mapping and several ways to implement it: <a href="http://arjan-tijms.omnifaces.org/2014/12/java-ee-authorization-jacc-revisited.html" target="_blank">Java EE authorization - JACC revisited part I</a>.<br />
The different, container specific ways to deploy a JACC provider and how to obtain the container specific role mapper are explained in the second part of the series: <a href="http://arjan-tijms.omnifaces.org/2015/01/java-ee-authorization-jacc-revisited.html" target="_blank">Java EE authorization - JACC revisited part II</a>.<br />
A fully working JACC provider will be presented in the upcoming third and final part of the series .<br />
<br />
<br />
Abhishek Gupta had a look at interceptors in Java EE in his article <a href="https://abhirockzz.wordpress.com/2015/01/03/java-ee-interceptors/" target="_blank">Java EE Interceptors</a>. If you like to get an overview on how to use interceptors and their history, you should take a few minutes to read this article.<br />
<div>
<br /></div>
<br />
<div>
<h2>
Upcoming online events</h2>
</div>
<div>
Lukas Eder will give a talk <a href="http://www.meetup.com/virtualJUG/events/219979734/" target="_blank">jOOQ: Get Back in Control of Your SQL</a> at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Tuesday+december+2nd" target="_blank">5pm UTC</a> on Thursday 29th at the vJUG.<br />
<br />
<br /></div>
<h2>
See you next week!</h2>
</div>
<div>
These were the most interesting resources I found this week. I hope you find them useful and I see you next Monday for the next issue. You can subscribe below to get an email notification for it or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.<br />
<br />
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-17780890043735380132014-12-15T19:23:00.000+01:002014-12-15T19:23:35.778+01:00Java 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.<br />
<br />
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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKiip_W6r1tv096NEzr3L0VJCjd95lxIirYaeIttJ2lJ_hI1UxhMm6m9l_XBbXI990Zc5OYBZ2jq35NlrO3ZQntjbCTswvvT7kOrYmgTfcKlYEUPtGS27Dpm45poR5j8q7qxpsMEe-D6GW/s1600/Java+Weekly+50.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
<div>
Martin Fowler explains in his article <a href="http://martinfowler.com/articles/replaceThrowWithNotification.html" target="_blank">Replacing Throwing Exceptions with Notification in Validations</a> why you should not throw Exceptions in most of your validation code. He also shows how to refactor your validation code to return a list with notifications for every detected error without breaking your existing applications.<br />
<br />
<br />
Marcus Lagergren wrote an interesting article about the <a href="https://blogs.oracle.com/nashorn/entry/nashorn_performance_work_in_the" target="_blank">improvements of the Nashorn engine</a> in the upcoming JDK 8u40 release. And if you like to learn more about the internals of the new type system, you should have a look at the linked resources at the end of his article.</div>
<div>
<br />
<br />
<h2>
Java 9</h2>
The modular run-time images of project Jigsaw are part of the new <a href="https://jdk9.java.net/download/" target="_blank">JDK 9 early-access build 41</a>. This change will have significant impact on developers and end users as Mark Reinhold wrote in his recent blog post: <a href="http://mreinhold.org/blog/jigsaw-modular-images" target="_blank">Project Jigsaw: Modular run-time images</a><br />
<br />
<br /></div>
<h2>
Java EE</h2>
<div>
Did you know that hibernate supports optimistic locking without a version column?<br />
Well, it does as Vlad Mihalcea shows in his recent post: <a href="http://vladmihalcea.com/2014/12/08/the-downside-of-version-less-optimistic-locking/" target="_blank">The downside of version-less optimistic locking</a>.<br />
This comes very handy if you need to support a legacy database schema but it also has some drawbacks.<br />
<br />
<br /></div>
<div>
<h2>
Java EE 8</h2>
</div>
<div>
Do you like to take a sneak peak at some features currently discussed by the CDI 2.0 expert group? Than you should have a look at the <a href="http://sourceforge.net/projects/jboss/files/Weld/3.0.0.Alpha3/" target="_blank">Weld 3.0.0.Alpha3</a> release and read the <a href="http://weld.cdi-spec.org/news/2014/12/10/an-update-on-weld-3/" target="_blank">accompanying post</a> by Jozef Hartinger. He explains how to get and use the new version of Weld and how to use the newest prototype for asynchronous events in CDI 2.0.<br />
<br />
<br /></div>
<div>
<h2>
This and that</h2>
The Java bean specification in the current version 1.01 is quite old. 17 years old to be precise. So it's obvious that it doesn't fit well into the current development. But how might a new version 2.0 look like?<br />
Stephen Colebourne explained his ideas for a new version of the specification in his recent blog post: <a href="http://blog.joda.org/2014/12/what-might-beans-v20-spec-contain.html" target="_blank">What might a Beans v2.0 spec contain?</a><br />
<br />
<br />
Trisha Gee wrote a great post about a big problem in our industry: The way we communicate with and listen to other people. Well, from my point of view this is not only a problem of our industry, it is a more general problem. But Trisha put her focus on software development teams in her article and that's ok. She explains how important it is to listen to all developers to create the best ideas and keep everyone motivated: <a href="http://www.javaadvent.com/2014/12/developers-want-to-be-heard.html" target="_blank">Developers want to be heard</a><br />
<div>
<br /></div>
<br />
<h2>
Upcoming online events</h2>
</div>
<div>
Dick Wall will give a talk <a href="http://www.meetup.com/virtualJUG/events/218827028/" target="_blank">Scala for Java Developers</a> at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Tuesday+december+2nd" target="_blank">5pm UTC</a> on Tuesday 16th at the vJUG.</div>
<div>
<br />
<br />
<h2>
See you next week!</h2>
These were the most interesting resources I found this week. I hope you find them useful and I see you next Monday for the next issue. You can subscribe below to get an email notification for it or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.<br />
<br />
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-53530849747006276012014-12-10T06:53:00.001+01:002015-04-19T06:14:14.157+02:005 ways to initialize lazy relations and when to use themLazy 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.<br />
<br />
Lets have a look at the different ways to trigger the initialization and their specific advantages and disadvantages.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHaohT9JIY30tmCjFuzLpzCXGWSs9g5d03-5iZMWAa3sMRcO-c1WZ0lvaIe2Wasu6jqdqxBOdgaFd17Bgzi8UtuftIRX1ZbBstLI2eHFYSq1tZa13MTd8JwuNp9O4CYuGbFp7bYW2PT4Jr/s1600/5.png" width="100%" /><br />
<br />
<a name='more'></a><br />
<h2>
1. Call a method on the mapped relation</h2>
Lets start with the most obvious and unfortunately also the most inefficient approach. We use the find method on the <a href="http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html" target="_blank">EntityManager</a> and call a method on the relation.<br />
<br />
<script src="https://gist.github.com/thjanssen/cadda834eaf1c44efc0b.js?file=mappedRelation.java"></script>
<br />
This code works perfectly fine, is easy to read and often used. So what is the problem with it?<br />
<br />
Well, you probably know it. This code performs an additional query to initialize the relation. That doesn't sound like a real problem but lets calculate the number of performed queries in a more real world-ish scenario.<br />
<br />
Lets say we have an entity with 5 relations which we need to initialize. So we will get 1 + 5 = <b>6 queries</b>. OK, that are 5 additional queries. That still doesn't seem like a huge issue.<br />
<br />
But our application will be used by more than one user in parallel (I hope). Lets say our system has to server 100 parallel users. Then we will get 100 + 5*100 = <b>600 queries</b>.<br />
<br />
OK, it should be obvious that this approach provides a working but not a good solution. Sooner or later, the number of additionally performed queries will slow our application down. Therefore we should try to avoid this approach and have a look at some other options.<br />
<br />
<br />
<h2>
2. Fetch Join in JPQL</h2>
A better option to initialize lazy relations is to use a JPQL query with a fetch join.<br />
<br />
<script src="https://gist.github.com/thjanssen/cadda834eaf1c44efc0b.js?file=fetchJoinJPQL.java"></script>
<br />
This tells the entity manager to fetch the selected entity and the relation within the same query. The advantages and disadvantages of this approach are obvious:<br />
<br />
The advantage is that everything is fetched within one query. From a performance point of view, this is much better than the first approach.<br />
<br />
And the main disadvantage is that we need to write additional code which executes the query. But it gets even worse, if the entity has multiple relations and we need to initialize different relations for different use cases. In this case we need to write a query for every required combination of fetch joined relations. This can become quite messy.<br />
<br />
Using fetch joins in JPQL statements can require a huge number of queries, which will make it difficult to maintain the code base. So before we start to write lots of queries, we should think about the number of different fetch join combinations we might need. If the number is low, then this is a good approach to limit the number of performed queries.<br />
<br />
<br />
<h2>
3. Fetch Join in Criteria API</h2>
OK, this approach is basically the same as the one before. But this time we are using the Criteria API instead of the JPQL query.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cadda834eaf1c44efc0b.js?file=fetchJoinCriteria.java"></script>
<br />
The advantages and disadvantages are the same as for the JPQL query with a fetch join. The entity and the relation are retrieved with one query from the database and we need specific code for every combination of relationships. But we often already have lots of use cases specific query code, if we are using the Criteria API. So this might not be a huge issue.<br />
<br />
If we are already using the Criteria API to build the query, this is a good approach to reduce the amount of performed queries.<br />
<br />
<br />
<h2>
4. Named Entity Graph</h2>
Named entity graphs are a <a href="http://www.thoughts-on-java.org/2015/02/jpa-21-overview.html" target="_blank">new feature of JPA 2.1</a>. It can be used to define a graph of entities that shall be queried from the database. The definition of an entity graph is done via annotations and is independent of the query.<br />
If you are not familiar with this feature, you can have a look at one of my <a href="http://www.thoughts-on-java.org/2014/03/jpa-21-entity-graph-part-1-named-entity.html" target="_blank">former blog posts</a> where I covered it in more detail.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cadda834eaf1c44efc0b.js?file=defineNamedGraph.java"></script>
<br />
The named entity graph can then be used by the find method of the EntityManager.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cadda834eaf1c44efc0b.js?file=useNamedGraph.java"></script>
<br />
This is basically an improved version of our first approach. The entity manager will retrieve the defined graph of entities from the database with one query. The only disadvantage is, that we need to annotate a named entity graph for each combination of relations that shall be retrieved within one query. We will need less additional annotations as in our second approach, but it still can become quite messy.<br />
<br />
Therefore named entity graphs are a great solution, if we only need to define a limited amount of them and reuse them for different use cases. Otherwise the code will become hard to maintain.<br />
<br />
<br />
<b>Bonus:</b> Get my free cheat sheet <a href="http://www.thoughts-on-java.org/2014/12/5-ways-to-initialize-lazy-relations.html#cheatSheet">about the new features in JPA 2.1</a>!<br />
<br />
<br />
<h2>
5. Dynamic Entity Graph</h2>
The dynamic entity graph is similar to the named entity graph and was also explained in <a href="http://www.thoughts-on-java.org/2014/04/jpa-21-entity-graph-part-2-define.html" target="_blank">one of the former posts</a>. The only difference is, that the entity graph is defined via a Java API.<br />
<br/>
<script src="https://gist.github.com/thjanssen/cadda834eaf1c44efc0b.js?file=dynamicGraph.java"></script>
<br />
The definition via an API can be an advantage and a disadvantage. If we need lots of use case specific entity graphs, it might be better to define the entity graph within the specific Java code and to not add an additional annotation to the entity. This avoids entities with dozens of annotations. On the other hand, the dynamic entity graph requires more code and an additional method to be reusable.<br />
<br />
So I recommend to use dynamic entity graphs, if we need to define a use case specific graph, that will not be reused. If we want to reuse the entity graph, it is easier to annotate a named entity graph.<br />
<br />
<br />
<h2>
Conclusion</h2>
<div>
We had a look at 5 different ways to initialize lazy relations. And as we have seen, each of them has its advantages and disadvantages. So what to remember from this article?</div>
<div>
<ul>
<li>Initializing a lazy relation via calling a method on a mapped relation causes an additional query. This should be avoided for performance reasons.</li>
<li>Fetch joins in JPQL statements reduce the number of queries to one but we might need a lot of different queries.</li>
<li>The Criteria API also supports fetch joins and we need specific code for each combination of relations that shall be initialized.</li>
<li>Named entity graphs are a good solution, if we will reuse the defined graph in our code.</li>
<li>Dynamic entity graphs can be the better solution, if we need to define a use case specific graph.</li>
</ul>
</div>
<div>
<br />
If you enjoyed this article and like to learn more about Java EE, make sure to subscribe below or follow me on <a href="https://twitter.com/thjanssen123" target="_blank">twitter</a> and <a href="https://plus.google.com/104438663880735057595" target="_blank">google+</a>.</div>Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-55428610570487892202014-12-08T07:12:00.001+01:002014-12-08T07:12:33.735+01:00Java 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.<br />
<br />
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.<br />
<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0iNAT5pNZqKPeTsKLwH0dWkZuv8qNOmo8uiOrOBbLpfwXzCIAvyemrqFS_ZAx9IZvvnUiwNpwpuZQH163-T-zjpPP17VRIQl13v2XOfXlNfyw2uwG2FjSZJ3rVGnYtD8_KOjpOWsdN821/s1600/Java+Weekly+49.png" width="100%" />
<br />
<a name='more'></a><br />
<br />
<h2>
Java</h2>
<div>
James Ward wrote the great post <a href="http://www.jamesward.com/2014/12/03/java-doesnt-suck-youre-just-using-it-wrong" target="_blank">Java Doesn’t Suck – You’re Just Using it Wrong</a> in which he names a list of things that often suck when doing Java development. Quite often the reason for it is not the language, it's the way we do our work. If you like to learn (or get reminded on) how to solve lots of common issues in Java development, you should read his article.<br />
<br /></div>
<div>
<br /></div>
<h2>
Java EE</h2>
<div>
Since annotations were introduced about 10 years ago with Java 5, their usage grew massively over the last years. If you have a look at a common Java EE application, everything is annotated. EJBs, CDI beans, entities, you name it. And most of the times two classes of the same kind (e.g. SFSBs) have the same annotations with very small differences in their values. What if we could group these annotations into one and reuse it wherever necessary?<br />
Roberto Cortez wrote a nice article about how you could do it and how clean the code would look afterwards: <a href="http://www.radcortez.com/annotations-annotations-everywhere/" target="_blank">ANNOTATIONS, ANNOTATIONS EVERYWHERE</a>.<br />
<br />
<br />
Timeout settings for REST service calls are not standardized by the JAX-RS 2.0 specification. Therefore we need to use proprietary features to create robust REST clients. Markus Eisele and Adam Bien show how to do this for a <a href="http://blog.eisele.net/2014/12/setting-timeout-for-jax-rs-20-resteasy-client.html">Resteasy client</a> and a <a href="http://www.adam-bien.com/roller/abien/entry/setting_timeout_for_the_jax">Jersey client</a> in their blog posts.<br />
<br />
<br />
Arun Gupta published the second issue of his hanginar series. This time he is talking with Lukas Eder about <a href="http://blog.arungupta.me/2014/12/jooq-jdbc-javaee-hibernate-hanginar2" target="_blank">jOOQ and how it relates to JDBC, Java EE, Hibernate, etc</a>.<br />
<br />
<br /></div>
<div>
<h2>
Java 9</h2>
</div>
<div>
The new <a href="http://openjdk.java.net/jeps/223" target="_blank">JEP 223</a> suggest a new versioning scheme for Java. If that JEP gets accepted for Java 9, the versioning scheme will FINALLY apply to the well known <a href="http://semver.org/" target="_blank">semantic versioning</a>!<br />
Read more about the proposed JEP and how Java version numbers might look like in future at <a href="http://www.infoq.com/news/2014/11/new-java-version-its-not-jdk19" target="_blank">New Java Version - it's not JDK 1.9</a>.</div>
<div>
<br />
<br />
It was proposed to drop <a href="http://openjdk.java.net/jeps/198" target="_blank">JEP 198: Light-Weight JSON API</a> from Java 9. Abhishek Gupta collected some information about in his post <a href="https://abhirockzz.wordpress.com/2014/12/05/heads-up-on-light-weight-json-api-jep-198/" target="_blank">Heads up on Light-Weight JSON API (JEP 198)</a>.<br />
<br />
<br />
<h2>
Books</h2>
I recently finished <a href="http://www.dpbolvw.net/click-7480317-11260198?url=http%3A%2F%2Fshop.oreilly.com%2Fproduct%2F0636920028499.do%3Fcmp%3Daf-prog-books-videos-product_cj_9781449358440_%2525zp&cjsku=9781449358440" target="_top">Java Performance: The Definitive Guide</a>*<img border="0" src="http://www.lduhtrp.net/image-7480317-11260198" height="1" width="1" /> and I like to recommend it to every experienced Java developer. It is one of the best books about Java performance I have read so far. If you like to learn more about it, have a look at my review: <a href="http://www.thoughts-on-java.org/2014/12/review-java-performance-definitive.html" target="_blank">Review "Java Performance: The Definitive Guide" by Scott Oaks</a>.<br />
<br />
<br />
You might have already seen it on some social media, but I want to mention it here anyway so that no one misses it. Packt Publishing has an <a href="https://www.packtpub.com/packt/offers/christmas-countdown" target="_blank">advent calendar</a> where they offer one selected book or video per day for free. Most of them so far were not about Java, but come on, most of us are looking into other stuff anyway ;-)<br />
<br />
<br />
<h2>
This and that</h2>
Are you wondering what Docker is about and if it is of some use for a normal Java developer?<br />
Then you should have a look at Oleg Shelajevs post <a href="http://zeroturnaround.com/rebellabs/docker-for-java-developers-how-to-sandbox-your-app-in-a-clean-environment/">Docker for Java Developers: How to sandbox your app in a clean environment</a>. He provides a short introduction to Docker and shows how to start your first Java application inside a Docker container.<br />
<br />
<br />
<h2>
Upcoming online events</h2>
</div>
<div>
Gavin King will give a talk <a href="http://www.meetup.com/virtualJUG/events/218826707/" target="_blank">Ceylon for Java Developers</a> at <a href="http://www.wolframalpha.com/input/?i=17%3A00+UTC+Tuesday+december+2nd" target="_blank">5pm UTC</a> on Tuesday 9th at the vJUG.</div>
<div>
<br />
<br />
<h2>
See you next week!</h2>
These were the most interesting resources I found this week. I hope you find them useful and I see you next Monday for the next issue. You can subscribe below to get an email notification for every issue.<br />
<br />
If you like to suggest a link for next weeks issue or something I can improve on, please write me a comment.<br />
<br />
<br />
<br />
* affiliate link</div>Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-34125325364267360092014-12-04T07:06:00.000+01:002014-12-11T20:08:53.574+01:00Review "Java Performance: The Definitive Guide" by Scott Oaks<div style="float: left;">
<a href="http://www.jdoqocy.com/click-7480317-11260198?url=http%3A%2F%2Fshop.oreilly.com%2Fproduct%2F0636920028499.do%3Fcmp%3Daf-prog-books-videos-product_cj_9781449358440_%2525zp&cjsku=9781449358440" target="_blank"><img alt="Java Performance: The Definitive Guide" border="0" src="http://akamaicovers.oreilly.com/images/0636920028499/cat.gif" /></a><img border="0" src="http://www.ftjcfx.com/image-7480317-11260198" height="1" width="1" /></div>
I recently finished the book <a href="http://www.dpbolvw.net/click-7480317-11260198?url=http%3A%2F%2Fshop.oreilly.com%2Fproduct%2F0636920028499.do%3Fcmp%3Daf-prog-books-videos-product_cj_9781449358440_%2525zp&cjsku=9781449358440" target="_top">Java Performance: The Definitive Guide</a>*<img border="0" src="http://www.lduhtrp.net/image-7480317-11260198" height="1" width="1" /> by Scott Oaks and I really liked it!<br />
<br />
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.<br />
I recommend it to every experienced Java developer who likes to learn more about performance optimization.<br />
<br />
But lets get into more details ...<br />
<a name='more'></a><br />
<br />
<h2>
About the author</h2>
Scott is working as an architect at Oracle. He is working on the performance of their middleware software.<br />
<br />
<h2>
About the book</h2>
The first and current edition of the book was released in 2014. It has 425 pages and is divided into 12 chapters.<br />
<br />
The first three chapters provide lots of introductory and methodically content. <b>Chapter 1</b> gives a short introduction into the book. In <b>chapter 2 and 3</b> Scott explains how to do performance tests and recommends several tools for it.<br />
<br />
This is followed by a good explanation of the JIT Compiler in <b>chapter 4</b>. After reading this chapter, you really know how the JIT compiler decides which code to compile and which to deoptimize. You also learn what you can do to tune it.<br />
<br />
I liked the following 4 chapters the most. They focus heavily on several memory related topics. <b>Chapter 5 and 6</b> provide very detailed information about garbage collection in Java, the different garbage collection algorithms and how to tune them. After that Scott discusses several best practices for the heap and native memory in the <b>chapters 7 and 8</b>.<br />
<br />
After the memory related topics are covered, Scott has a detailed look at threads in <b>chapter 9</b>. He shows how to define good pool sizes, explains the synchronization costs that occur in a multi-threaded environment and introduces some tools to monitor the threads of an application.<br />
<br />
Performance tuning in the Java EE environment is explained in <b>chapter 10</b>. He talks about performance of the web container, EJBs, XML and JSON processing, object serialization and networking APIs. This chapter contains solid information but it is the weakest from my point of view. The performance and the tuning options are highly depend on the implementation of the application server and its components. Therefore this book covers only a few, general tuning options and concepts. You should have a look at an application server specific tuning guide, if you need more detailed, server specific information.<br />
<br />
<b>Chapter 11</b> covers JDBC and JPA related best practices. Scott has a look at several options to tune applications your application.<br />
<br />
Java SE related tuning options are explained in <b>chapter 12</b>. Some of the 11 different topics discussed in this chapter are Classloading, the Java Collection API, Lambdas and Streams.<br />
<br />
<h2>
Who should read it?</h2>
<div>
This book is not for Java beginners and it is no easy read. Don't get me wrong, it is very well written! But the covered topics are complex and the author gives lots of in-depth explanations about them. </div>
<div>
You should definitely be an experienced Java developer to get the most out of this book.</div>
<div>
<br /></div>
<h2>
Would I recommend it?</h2>
<div>
<b>YES!</b> This book provides some of the most detailed information about Java performance and especially garbage collection that I have read so far. If you want to get a deeper understanding of these topics and bring your Java skills to the next level, this is the right book for you!<br />
<br />
<h2>
Where to get it?</h2>
Well, you can get it in every book store (e.g. <a href="http://amzn.to/1yDDK23" target="_blank">Amazon.com</a>*). But I prefer to buy it as a <b>DRM free ebook</b> at oreilly.com: <a href="http://www.dpbolvw.net/click-7480317-11260198?url=http%3A%2F%2Fshop.oreilly.com%2Fproduct%2F0636920028499.do%3Fcmp%3Daf-prog-books-videos-product_cj_9781449358440_%2525zp&cjsku=9781449358440" target="_top">Java Performance: The Definitive Guide</a>*<img border="0" src="http://www.lduhtrp.net/image-7480317-11260198" height="1" width="1" />.<br />
<br />
<br />
<br />
<br />
Disclaimer: I got this book from o'reilly for free to write a review.<br />
<br />
* affiliate link
</div>
Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0tag:blogger.com,1999:blog-5355106262763766945.post-90023245588109675072014-12-02T08:21:00.000+01:002014-12-02T08:21:15.436+01:00Java Magazin published my first articleHey,<br />
<br />
I just wanted to tell all german readers, that the <a href="https://jaxenter.de/Java-Magazin/Java-Magazin-115-177685" target="_blank">Java Magazin</a> published my first article in the current <a href="https://jaxenter.de/Java-Magazin/Java-Magazin-115-177685" target="_blank">issue 1.15</a>. 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.<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmb7EPMXNuSFlg2jQbM2gMIHkqzfFR_HNr0uFs-HyBqEfzRyVtJjdt2Y-nQxhgENMUcs62X2YV-ZHqpoBrdsADh8jL18K5gIWJCFFKtSC8ffSl22pjdGxovvZdBNXKBUAMhiknYw3cvyXv/s1600/JM1-15.jpg" width="100%" />
<br />
<br />
You don't speak german and like to learn more about Attribute Converter? No problem, I also wrote several blog posts about it:<br />
<br />
<ul>
<li><a href="http://www.thoughts-on-java.org/2013/10/jpa-21-how-to-implement-type-converter.html" target="_blank">JPA 2.1 - How to implement a Type Converter</a></li>
<li><a href="http://www.thoughts-on-java.org/2013/10/jpa-21-type-converter-better-way-to.html" target="_blank">JPA 2.1 Type Converter - The better way to persist enums</a></li>
<li><a href="http://www.thoughts-on-java.org/2014/06/how-to-use-jpa-type-converter-to.html" target="_blank">How to use a JPA Type Converter to encrypt your data</a></li>
</ul>
<br />
<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/04147076428817122840noreply@blogger.com0