the force that pushes object up
Random header image... Refresh for more!

Java Puzzlers: Phantom Reference Menace, Attack Of The Clone, & Revenge Of The Shift

In this presentation, Joshua Bloch and  William Pugh describes eight short Java programs with curious behavior. Things discussed in this presentation will help you a lot, if you are a Java developer.

Following is the list of problems and summary of things you can learn from studying those problems.

1. The Joy Of Sets

What is the out put of following program.

Moral of the above problem:

  • Collection<E>.remove takes Object, not E
  • Integral arithmetic always results in int or long
  • Avoid mixing types
  • Avoid short; prefer int and long

2. More Joy of Sets

Out put of the above program will depends on the whether you are connected to internet or not. This happens because URL’s equals and hashCode are broken.

Things we can learn from above problem:

  • Do not use URL as a Set element or Map key
    • equals and hashCode aren’t well defined
    • They do not obey their general contracts
  • Use URI instead of URL
    • Make URL from URI as neccessary
  • equals should not depends on the environment

3. Racy Little Number

How often does the following test pass.

Moral of the problem:

  • JUnit does not support concurrency
  • You must provide your own way of handling concurrency
    • If you don’t you will get a false sense of security

4. Elvis Lives Again

What does following program print?

Moral of the problem:

  • Wrapped primitives aren’t primitives
    • Prefer primitives to wrapped primitives
  • Auto-unboxing can occur when you leas expect it
    • It can cause NullPointerException
  • Never use Boolean as a three-valued return type
    • Almost guarantees NullPointerException
  • Watch out for circularities in class initialization
    • Construct instance at end of class initialization

5. Mind The Gap

What does following program prints?

Moral of the problem:

  • The skip method is hard to use and error prone.
  • Use your skipFully(you will learn about it during the presentation) instead of skip
  • More generally, if an API is broken, wrap it
  • For API designers
    • Don’t violate the principle of last astonishment
    • Make is easy to do simple things

June 27, 2010   No Comments

JPA Implementation Patterns

These days I am working on Human Task implementation for WSO2 Carbon platform, and decided to use JPA based persistence mechanism. While working on ‘Task Engine’ implementation, I found out that JPA is not just about implementing set of entities and persisting them using ‘Entity Manager’. I found out that, there are small but very important things that anyone should know to make their JPA based implementations successful. Without those you’ll stuck in the middle of some thing, deciding what is the best option. While googling I found series of articles from Vincent Partington on JPA implementation patterns which are very useful for anyone who new to JPA. In this post I am going to summarize each and every post from ‘JPA Implementation Patterns‘ article series.

Data Access Objects

This post starts with the background information about JPA and JPA implementations. After that Vincent tells us that there are more things than data access object and domain objects, like transaction handling, lazy loading and detach objects.

Then he comes to the main topic and first describes why we need DAOs(Data Access Objects) and how DAO layer which resides on top JPA can help you. After that he provides us with the information about type-safe generic DAO pattern, how to use DAO and advantages of type-safe generic DAO pattern.

In think this post is a must read for anyone who try to get started with JPA and this post help me a lot to understand issues in my JPA based implementation.

Bidirectional Associations

This post is about association between JPA entities. And it mainly describes a bidirectional association pattern used by developers who use JPA. Vincent start this post with a sample and then describes how bidirectional association can make developers life easier. After that he describes problems associated with bidirectional association and provide reader with a patter that can use to solve the issues. At last he describes the variations to the patter the introduced and what user must consider before using this pattern.

Saving (detached) Entities

This post talks about saving detached entities or merging changes done to the detached entities. First Vincent talked about how entities become detached and what  will happen when try to persist detached entity.

Then he describes about differences between Hibernate’s saveOrUpdate and JPA Entity Manager merge method.

In the last part of this post Vincent describes the problems with JPA merge and a pattern which every one can use to solve issues described with merge method.

Retrieving Entities

This post talks about two ways of retrieving an entity. One is using EntityManager.find and other is using JPQL queries. This post contains three example ways of retrieving entities by using EntityManager.find and JPQL queries.

Removing Entities

This post mainly focused on removing entities with bi-directional relationships. And Vincent has described pattern to overcome issue with removing entities when there are bi-directional associations.

Service Facades and Data Transfer Objects

This post is mainly about applying old school enterprise application architecture patterns to your JPA base implementations. First Vincent describes why we must bother about DAO’s DTO’s and Service Facades. Then he describes pros and cons of Data Transfer Objects(DTO) and pros and cons of Service Facades in JPA space. Finally he explain to you implications to your JPA architecture when you use DTO’s and Service Facades.

Lazy Loading

To use JPA to its full potential you must understand ‘Lazy Loading’ and how it works. Understanding lazy loading will allow you to utilize computing resources effectively and help you to design you entities and their relationships in way that it increase the efficiency.

First part of the post is about lazy loading and when lazy  loading occur. Then post has some information about lazy loading implementation mechanisms. After that the post describes “Runtime proxy based lazy loading” in hibernate and “Runtime bytecode instrumentation” in OpenJPA. Then there is a seperate section on differences between Hibernate and OpenJPA. Finally post contains a patter which help us to effectively use lazy loading.

January 30, 2010   No Comments