October 05, 2009

How to Customize the Eclipse Outline

What's your first action after opening an unknown class file with the Eclipse IDE? I for myself first inspect the Outline view to get a general idea about the involved members and methods. It's totally crucial to me to optimize the contents of the Outline view, thus minimizing the time to getting started with unknown sourcecode. Let's take a look how to improve the Outline view.


October 01, 2009

How to log Hibernate Cache Hits

Hibernate enables you to improve the performance of your application by using second-level caching. What does this mean? The second-level cache is capable of caching hibernate entities even when the hibernate session closes.
Before loading an entity from the database the session first checks if the entity has already been cached. This can greatly reduce the number of database accesses and thus improve the performance on each cache hit.


September 25, 2009

5 Tips for Customizing Eclipse

The Eclipse IDE not only is one of the most popular IDEs. It's also a very complex tool with countless features and commands. Knowing most of these features is crucial for developers. The following articles contains five tips for customizing and tweaking the Eclipse IDE.


September 14, 2009

Introduction to Google Collections

Did you ever felt that working with the Java Collections Framework could be more elegant or efficient? Then you really should consider to use the Google Collections API. It's a great utility library every Java developer should know. Take the time to read this introduction to easily getting started with Google Collections.

The Google Collections Library 1.0 is a set of new collection types, implementations and related goodness for Java 5 and higher, brought to you by Google. It is a natural extension of the Java Collections Framework you already know and love.

September 02, 2009

Hibernate Preload Pattern

This article introduces an approach to preload certain relations in complex object graphs with Hibernate on a per-usecase basis. The intention is to prevent LazyInitializationExceptions during runtime and to reduce the N+1 SELECT problem while working with lazy relations. What does per-usecase mean in this context? The approach affords to easily decide which parts of an object graph are directly loaded by Hibernate for each and every usecase . If you're familiar with these problems you can skip the next section and dive directly into the proposed pattern.


August 28, 2009

Hibernate 3.5 Beta1 with JPA 2.0 released


The first beta of Hibernate 3.5 with JPA 2.0 support has been released on Sourceforge. It's also available as maven2 artifact from the JBoss maven repository. As Steve Ebersole mentioned most of the APIs have already been implemented.


August 25, 2009

Great Code Navigation Plugin

Code Navigator is an amazing award-winning plugin for IntelliJ IDEA. This tool makes graphical navigation in object-oriented code easy and comfortable.


August 24, 2009

Optimize Eclipse in 5 Steps

As one of the most popular IDEs for developing Java applications, Eclipse can be adjusted with plenty of preferences. Here are my five favorite adjustments to tweak the Eclipse IDE. It works well with either the newest Eclipse Galileo or one of the older releases like Ganymede or Europa.


August 20, 2009

Building equals(), hashCode(), compareTo() and toString() with ease

Implementing equals(), hashCode(), compareTo() and toString() is a common task for java programmers. If you're unfamiliar with this topic, check out Joshua Blochs best practices on how to override hashCode().
Modern IDEs like Eclipse offer commands to auto-generate these methods for particular classes. While this is ok in terms of time saving, the generated code is verbose and therefor hardly maintainable.


August 18, 2009

Introduction to JMockit

Using mock-objects is crucial when it comes to unit-testing enterprise applications. Mocks prevent you from implicitly testing parts of your application many times. This not only leads to faster running times of the testsuites. It also improves time to find the cause of a bug on failure.
Let me introduce to you JMockit, a Java mocking library which integrates nicely with JUnit and TestNG. As an open source library JMockit is released under the MIT license. It heavily relies on bytecode instrumentation, first introduced in Java SE 5.
What does this mean? JMockit directly wires the mock-objects into the applications bytecode at runtime. Therefor it's possible to mock any object even those not implementing any interface. This is one of the main advantages over proxy-based mocking libraries.


August 17, 2009

Builder Pattern Code Generator for Eclipse

In his blog Lars started a series of articles about using the builder pattern (read here and here). He describes the main benefits of using this pattern for creating simple and complex data structures.
I'm asking myself: why is this pattern so rarely used? In my opinion this is due to the fact that it really bloats your code. I surely agree that the domain model classes are one of the most important parts of an application. But writing builders for each of these model classes is plain boring.


August 15, 2009

Best way to avoid LazyInitializationException in Hibernate

Did you ever have to bother with LazyInitializationExceptions in hibernate? I'm sure you have. :) This famous exception occurs if you try to access any non-initialized assocation (or proxy) when the assigned hibernate session has already been closed.
There are many solutions for this problem, e.g. fetching the association directly via the hibernate query language. Think about a hibernate mapped entity Customer which has a one-to-many relation to an Order entity. Both classes are mapped to different database tables with a foreign key constraint.


August 14, 2009

How to query several columns with Hibernate?

Even though Hibernate is called an object-relational mapper it's easy to select only a few properties instead of the whole mapped object. The hibernate query language (HQL) is very expressive. If you select some properties instead of the whole object, the call of Query#list() returns a list of object arrays. Each field of the array contains one of the selected properties.


Import madness

Do you know the intersection between the Google Collections API and TestNG? Both APIs contain a class Lists with two identical methods newArrayList(). No problem so far, but you should really pay attention which one to import. If you set up your project with Maven2 it's best practice to use the test scope on each test-specific dependency. That means wrongly importing org.testng.v6.Lists in productive code would leed to some weird compiler errors on running the build scripts. So pay attention to import com.google.common.collect.Lists instead of the one from the TestNG API.

August 13, 2009

Top 10 Eclipse Shortcuts

Here are some of my most used keyboard shortcuts for the Eclipse IDE.
  • Code Assist (CTRL + Space)
  • Quick Fix (CTRL + 1)
  • Refactoring (ALT + SHIFT + T)
  • Source (ALT + SHIFT + S)
  • Surround With (ALT + SHIFT + Z)
  • Delete Rows (CTRL + D)
  • Call Hierarchy (CTRL + ALT + H)
  • Quick Type Hierarchy (CTRL + T)
  • Quick Outline (CTRL + O)
  • Show All Shortcuts (CTRL + SHIFT + L)

Separation of Concerns: The proxy pattern

The principle of Separation of Concerns is one of the main aspects of modern application frameworks like Spring or Hibernate. The intention is to separate the cross-cutting-concerns (e.g. database access, transaction management or security checks) from the implementation of the functional requirements.
One possible solution to realize a transparent separation of concerns is to use the proxy design pattern. A proxy is some kind of wrapper, which controls the access to the interface of any object. Therefore the proxy implements the same interface as the wrapped object. By using the proxy pattern you're able to extend or change the behavior of an object without changing the object itself.


August 12, 2009

3 ways of integrating Hibernate and Spring

There are many ways to integrate Hibernate Data Access Objects (DAOs) with the Spring Framework. The Spring 2.5 documentation describes three possible solutions how Spring supports those DAOs. But which is the right way to go nowadays? Let's compare the three variants:


How to use code templates in Eclipse?

If you are using the Eclipse IDE you can speed up your development time by using the code template feature. Per default code templates are accessible by cycling through the code assist (CTRL + Space) directly from the source code editor. Each code template have one or many keywords assigned, e.g. if you're applying code templates onto the keyword sysout Eclipse replaces the keyword with System.out.println().


Eclipse 3.4: Target plattform issue

There is a problem with some distributions of Eclipse Ganymede relating the PDE target plattform. If you want to set up the target plattform (see Preferences -> Plug-In Development) you cannot add Plugins to the list via the Add... command. There is no error dialog, the list of plugins just stays empty.
You can fix this issue by installing another Plug-In called Eclipse Provisioning Plug-In Development. It's available from the preset Eclipse Ganymede update site.

Observer Pattern revised: The EventBus

The EventBus library is a convenient realization of the observer pattern. It works perfectly to supplement the implemention of MVC logic (model-view-controller) in event-driven UIs such as Swing. The library is similiar to the JMS API and it's published under the Apache License 2.0.
The Event Bus is a single-process publish/subscribe event routing library, with Swing extensions. The EventBus is fully-functional, with very good API documentation and test coverage (80+%). It has been deployed in many production environments, including financial, engineering and scientific applications.


How to improve drag-and-drop behavior in Swing?

The class javax.swing.TransferHandler is a convenient solution for implementing drag-and-drop in a Java Swing UI. You don't have to bother about the internal concepts of drag-and-drop in Swing.
Unfortunately there are issues with the implementation of the TransferHandler in JDK 1.4 and JDK 5. If you want to drag an element from JTable, JTree or JList, the element must be selected first. That means it's not possible to select a new element with one click and at the same time drag the element to another component.