Posts in category 'Java'


Running Java Unit and Integration Tests Separately

Eclipse and Maven are not designed from the ground up to run automated integration tests separately from unit tests. This is a problem because integration tests typically take longer to run, so when coding, especially if doing test-driven development, there is the need to frequently run just the unit test suite from Eclipse. Maven by […]

Java Code Examples Available

I have added a new project called Java Examples to my Software page. This project contains the source code used as examples in the following articles: Working with Java 5 Annotations Parsing and Generating XML with Java Advanced Uses of Java 5 Enums A Tale of Bad Exception Handling in Finally Blocks in Java Automatically […]

Java-Based Configuration of Spring Dependency Injection

At the heart of the Spring Framework is its dependency injection capabilities provided by its inversion-of-control container. Traditionally the configuration of dependencies has been done in one or more separate XML files. In these files you need to specify code-specific constructs such as the concrete classes to use as implementations of interfaces. It has always […]

Simple XML Parsing using JAXB

I recently needed to parse a XML file using Java for a utility I was writing. A couple of years ago I used dom4j to parse XML (and wrote an article about it). I figured there had to be a more modern approach, similar to how Hibernate 3.0 can map POJOs (plain old Java objects) […]

Working with Java 5 Annotations

Annotations are a new language feature introduced in Java 5 that allows Java code elements such as classes or methods to be annotated with structured metadata. This metadata can then be used at compile-time or at run-time by other code. Annotations are commonly used to provide configuration information for infrastructure frameworks to provide cross-cutting functionality. […]

Creating Custom Tags Using JSP

Java Server Pages (JSP) is a Java technology for rendering dynamic web pages. Unlike servlets written directly in Java, JSP files contain special markup to identify java or JSP code. The normal (non-markup) text is interpreted as HTML. Early in JSP’s evolution, custom tags were introduced as a way of providing reusable functionality, particularly reusable […]

Automatically Populating Audit Columns in Hibernate

Audit columns are a common design pattern used to record data creation and modification information for database tables. A typical implementation of this pattern is to add four columns to every non-static database table: CREATE_USER, CREATE_TIMESTAMP, UPDATE_USER, and UPDATE_TIMESTAMP. The create columns are populated only when a record is initially populated, while the update columns […]

Improving Performance via Eager Fetching in Hibernate

My previous article started discussing Hibernate relationships, focusing on lazy versus non-lazy relationships. This article continues the theme by discussing how to improve performance when dealing with relationships in Hibernate through a feature called eager fetching. Hibernate’s abstraction of database access behind getter and setter methods on domain objects hides potentially inefficient database access. Mindlessly […]

Avoid Non-Lazy Relationships in Hibernate

Support for entity relationships is a great time-saving feature in Hibernate, but it can also be a trap for the unsuspecting developer. Handling relationships between entities can be a complex business, and I for one am glad for all the support that Hibernate provides. Hibernate’s assistance, however, can do more harm than good when it […]

Hibernate and Logging

Hibernate tries to hide the details of dealing with relational databases, but it is at best a leaky abstraction. At its most basic level, Hibernate is a framework that issues SQL commands to the database. Sometimes it does not do what you would expect or want (more on that in future articles). Therefore it is […]

Hibernate Tips and Tricks

Hibernate is a de facto standard for object-relational mapping. One of my recent projects involved the use of the latest version of Hibernate (3.2). Since I had not used Hibernate since its version 2 days, I picked up the authoritative reference Java Persistence with Hibernate which is co-authored by Gavin King, the founder of Hibernate. […]

A Tale of Bad Exception Handling in Finally Blocks in Java

There is always something more to learn. That was the lesson for me last week when I learned something new about the Java programming language, despite having used professionally it for almost 10 years. I was upgrading a Java web application to WebSphere server version 6.1 and as the first step I switched the development […]

Tips for Using Log4j

Log4j is a logging framework for Java, part of Apache’s suite of logging frameworks for various languages. While using log4j on various applications, I have had to figure out how to do certain things that were not clearly documented, possibly because the full manual for log4j is only available commercially. Before showing what I have […]

Advanced Uses of Java 5 Enums

Enumerated types – called enums for short – were introduced to the Java language recently in version 5 (the version after 1.4). The name essentially defines what they are: a type that has a fixed set of constant values. The Java Sun website has a good tutorial on using enums which covers their basic capabilities. […]

Parsing and Generating XML with Java

December, 2008 Update: I now recommend using JAXB and annotated Java objects to parse and generate XML. See my article Simple XML Parsing using JAXB for details. I’ve worked a lot with XML over the years (i.e. for Ant build files), but I’ve never had to parse or generate XML in Java until recently. There […]

Understanding Java RMI: A Simple Tutorial

I recently needed to write a simple java client-server application, and decided to implement the communication mechanism using Java RMI (Remote Method Invocation). I’ve used RMI in the past indirectly (i.e. when coding EJB session beans), but never directly, so I turned to Google to see what RMI tutorials were available. I quickly found several […]

Local Variable Declarations

When coding in Java, I prefer to declare local variables at their point of use. On a recent project, I have encountered developers who have been taught in their computing science course(s) to put local variable declarations at the start of the method (when using Java). Back in the days of C this was a […]