Thursday, August 27, 2009

Using TINY_INT for Java Boolean types with Hibernate

>>We are using Spring 2.0 and Hibernate 3.2 with a MySQL 5 database.

THE PROBLEM: By default Hibernate persists properties that are of Java Boolean or boolean type as CHAR type with values 0 and 1 in our MySQL database. This is efficient, but when we want to look at the database using our GUI tools, these values both display as the same meaningless box character. This is because the ASCII values 0 and 1, both represent non-printable characters.

THE SOLUTION: If we force Hibernate to make these columns TINY_INT type, our tools will display the values correctly as “0” and “1”. Hibernate allows us to do this by creating a custom type and assigning it to all of our boolean properties. Here’s how:

1. Create a custom Hibernate type. We extend Hibernate’s abstract BooleanType and override a few methods to customize it for our needs.

package com.example.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.dialect.Dialect;
import org.hibernate.type.BooleanType;

public class OneZeroBoolean extends BooleanType
private static final long serialVersionUID = 1L;

public Object get(ResultSet rs, String name) throws SQLException
if (rs.getObject(name) == null)
return null;
int code = rs.getInt(name);
return code != 0;

public void set(PreparedStatement st, Object value, int index) throws SQLException
if (value == null)
st.setObject(index, null);
st.setInt(index, Boolean.TRUE.equals(value) ? 1 : 0);

public int sqlType()
return Types.TINYINT;

public String objectToSQLString(Object value, Dialect dialect) throws Exception
return ((Boolean)value).booleanValue() ? "1" : "0";

public Object stringToObject(String xml) throws Exception
if ("0".equals(xml))
return Boolean.FALSE;
} else
return Boolean.TRUE;

2. Register this new type with Hibernate. We are using annotations for our hibernate configuration, so this is done in the file in the package where our entity classes are defined. Here register our new type under the name “onezero-boolean”. You can use any name you like (unless it’s already used by hibernate). We will reference this name later when we declare the boolean properties themselves.

@TypeDefs( { @TypeDef(name = "onezero-boolean", typeClass = OneZeroBoolean.class) })
package com.example.model;

import org.hibernate.annotations.TypeDefs;
import org.hibernate.annotations.TypeDef;

NOTE: In order to use these package-level annotations we need to tell the Hibernate configuration to look for them. Your configuration may be in hibernate.cfg.xml or you may be doing it in Spring. Here are examples from both…

Hibernate.cfg.xml: Add a mapping within <session-factory> telling hibernate to look for package-level annotations in the specified package.


<mapping package="com.example.model" />

Spring: Spring provides some helpers to configure a hibernate session factory as an alternative to specifying everything in hibernate.cfg.xml. In Spring 2.0 it’s the org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean class. Here is a snippet of the configuration including the extra piece needed for package annotations:


<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- This property tells it to look at our package-level annotations -->
<property name="annotatedPackages">
<property name="annotatedClasses">

3. Where ever boolean or Boolean type properties are declared in the entity classes, tell hibernate to use our new custom type. This is done with the hibernate @Type annotation; just specify the name that we used to register our custom type above.

package com.example.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.Type;

public class Entity1
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

private boolean active;

//Getters and Setters

That’s all there is to it. This same type can be applied to any and/or all boolean properties in our entities. Hibernate schema generation will generate TINY_INT columns for us and hibernate persistence will store boolean values as 1(true) and 0(false) in those columns.

Wednesday, August 26, 2009

pair programming

Here are a steps I would like the team to follow while developing:

1. Create a wiki topic on what you are planning to do and explain these steps to someone before starting writing the code
2. Have a buddy spend at least a part of the day pair-programming with you, return the favor the next day.
3. Make sure to do a code-review daily and explain how the features work
4. Test your buddy's code and have them test yours. Use the wiki list as the start
5. Post screenshots on wiki early on and have client read thru them, make sure you get feedback (so you know they have read it)
6. Update the test list for the project
7. Assume that UAT testing is NOT for developers but for the CLIENT, there should be no surprises or code changes but then.

This list came up from the our experience and feedback from my developers (thanks to Phil W. for your thoughts)...

In the past development cycle I cave in to the fact that we had a large number of tasks and a limited, geo-dispersed team so I abandoned the practice of pair programming. The result was a prolonged period of
"bug fixes" and late, stressful build.

With the pair programing, we sometimes had a few of issues, but we never spent so much time on fixing bugs. It was almost a deja vu of the previous projects, of which many have failed.

As a project manager I have to take a full responsibility for this failure, and correct the problems.

Pair programming fosters:

- better understanding of the "big picture" by the whole team
- essential questions asked early on in the development cycle
- lack of procrastination with development
- more time spent actually developing vs. browsing the web, or daydreaming

Single developer programming leads to:

- lack of team work
- developers working on hard issues burn out and loose motivation
- developers "sand bag", or inflate the time required to accomplish the task
- developers take it "personally" and get hurt emotionally when client criticize their work
- when the developers finally quits, no one knows how things work

Monday, August 24, 2009

Google Wave: capabilities.xml version

As I was developing my first robot on Google Wave, I encountered a bug where my robot's capabilities are not recognized when I deployed it to Google App Engine. The reason for this bug is due to the fact that the robot's capabilities are cached on the robot proxy server. If you do not update the version in capabilities.xml, the robot will use the cached information and you on the other hand will be wondering why my robot ain't working properly!

The best way to think of the version tag in capabilities.xml, is to use it as a tool to mark the milestones in your robot's life cycle. Therefore version 1 for instance will correspond to hello world, and version 2 will correspond to a new capability that you may add such as name="document_changed", etc. Also, it is a good idea to keep the version in capabilities.xml same as version number in appengine-web.xml just to make things simple, though this is not required. Note that Google App Engine will allow you to deploy a single app 12 versions only, so you will have to delete the unneeded versions there.

Friday, August 14, 2009

GWT Image Dimensions and Pre-loading

I've run across this problem several times when programming in both JavaScript and in GWT (Google Web Toolkit). I need to fit an image in a specific space while maintaining aspect ratio, but I don't know the image dimensions.

For example I have a 100px by 100px space in my layout for a users profile picture, but the actual picture might be 300 x 200 or 200 x 400 or any other size. So half the time need to contrain the width of the image to get the correct aspect ratio, and half the time the height.

Luckily, we can count on browsers supporting the image "onload" event which is fired when the image fully loads. If we have not given the image any size constraints, the browser will display the image full size and we will be able to get it's dimensions at that point. An finally we can use those to resize it.

This method works, but it can get pretty complicated to try to hide the image so that it doesn't mess up the layout while it's loading and deal with race conditions that come with cashed images as well as several other cross-browser support issues. To make all of this much easier for us all, there is an open-source utility written for GWT that wraps all of the messy details in a simple and reliable interface.

The utility can be found at There are good examples on the site, but here's a preview.

To solve my problem of the 100px by 100px profile space, I have two options.

1) Have the ImagePreloader tell me what the image dimensions are and then resize my profile picture.

ImagePreloader.load(<IMAGE URL>, new ImageLoadHander() {
public void imageLoaded(ImageLoadEvent event) {
if (!event.isLoadFailed()) {
int originalWidth = event.getDimensions().getWidth();
int originalHeight = event.getDimensions().getHeight();

//add my logic here to resize image to fit in 100x100 space

2) Let the utility take care of this for me. Use the FitImage class which is a subclass of Image that allows me to set the maximum width and height, just like I want to do in my example. The image will automatically resize it self to fit within the 100 by 100 box while maintaining aspect ratio.

FitImage image = new FitImage(<IMAGE URL>, 100, 100);

If this sounds like something you could use, check it out ( It's free and open source.

GTUG3 Agenda

Google Technology Conference #3 in Chicago Agenda on Google Docs (spreadsheet):

Wednesday, August 12, 2009

Tuesday, August 11, 2009

Sunday, August 9, 2009

Installing Java SE 6 on Mac OS X 10.5.7

To install Java 1.6 (Java 6 is required for Google Wave development)

- Create a Mac developer account

- Download:
- Include this classpath in your .bash_profile file located on /Users/your-name/.bash_profile:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
export PATH=$JAVA_HOME/bin:$PATH

- Verify that installation was successful, run below command in your terminal to verify that you have Java 1.6.0:
java -version

Here is my output:
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-223)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-92, mixed mode)

Friday, August 7, 2009

Transparency with CSS and GWT

It's simple to make transparent elements in HTML. Just use CSS. Even IE6 supports it.

Say we want an image to have 25% transparency...

The standard CSS property is

opacity: 0.25;

but IE and some older browsers have different ways of specifying it, so you probably want to use them all to cover your bases

opacity: 0.25; //css standard
filter:alpha(opacity=25); //ie (note that 50% is 25 here, not .25)
-moz-opacity:0.25; //very old mozilla/netscape browsers
-khtml-opacity: 0.25; //very old safari browsers

Here is what it might look like in GWT

public void setOpacity(Element element, double opacity) {
if (opacity > .995) {
element.getStyle().setProperty("opacity", "");
element.getStyle().setProperty("filter", "");
element.getStyle().setProperty("-moz-opacity", "");
element.getStyle().setProperty("-khtml-opacity", "");
} else {
String s = Integer.toString((int) Math.round(opacity * 100));
String sDecimal = (s.length() == 1 ? ".0" : ".") + s;
element.getStyle().setProperty("opacity", sDecimal);
element.getStyle().setProperty("filter", "alpha(opacity=" + s + ")");
element.getStyle().setProperty("-moz-opacity", sDecimal);
element.getStyle().setProperty("-khtml-opacity", sDecimal);

Tuesday, August 4, 2009

Podcast: eHarmony

I think this is a really worthwhile podcast.

MailServe for Mac

MailServe is a great application if you want to send email from your Mac

It is wise to STOP it when you don't need to use it, for that OPTION-click on "Restart Postfix" icon in the TOP RIGHT, then make sure all the lights are RED on the bottom.

Monday, August 3, 2009

Ethernet vs. WI-FI vs. VPN

I was testing the speed of my Comcast cable wondering how much the WI-FI and VPN connection slow me down and the results actually suprised me:

Ethernet directly via cable
15.6 down
6 Mbps up

Ethernet with VPN connected:
15.6 Mbps down
6.6 Mbps up

WIFI (via Apple Airport Base Station 802.1g, not "n" )
16.0 down
5.8 Mbsp

As you can see the speed is the same (+/- small variation), so being tethered on the cable does not help anything really.

Finally AT&T 3G card:

1.845 Mbps download speed

0.966 Mbps upload speed

As you can see the 3G is NOT a replacement for Cable.

Java ImageIO and GIF

I'm working on a project where users can upload images in JPEG, PNG and GIF formats and we resize the images using Java's built-in ImageIO API. Unfotunately, there's some confusing issues with GIF support. Using Java 1.5, GIF images can easily be read, but writing GIFs is not supported. This apparently has to do with CompuServe's patent on the GIF format.

The good news is that the patent recently ran out, so we can now freely write images to GIF format with ImageIO.

We have two simple options to make this work--either upgrade our project to Java 1.6 or include a plugin JAR that adds GIF support (for example com.sun.imageio.plugins.gif for Java 1.4 and above).


iPhone used for new Nissan car remote

Saturday, August 1, 2009

AppEngine data viewer in hosted mode

If you are frustrated that you cannot view your data in the AppEngine GWT in hosted mode, follow these steps:

Update Google AppEngine SDK to the newest version (Java 1.2.2 as of this writing):


The next (most alloying step) is to insert at least one record for each object you want to save:

public UserDao()

private void init()
User user = fetchFacebook(new Long(764XXXXXX));
if (user != null)

User uki = new User("Uki D. Lucas");
uki.setFacebookUID(new Long(764XXXXXX));

There is a List and Delete options, I suppose the Edit and Add are coming soon.