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.

OneZeroBoolean.java

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);
else
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 package-info.java 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.

package-info.java

@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.

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
...
<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:

applicationContext.xml

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<!-- This property tells it to look at our package-level annotations -->
<property name="annotatedPackages">
<list>
<value>com.example.model</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>com.example.model.Entity1</value>
<value>com.example.model.Entity2</value>
...
</list>
</property>
</bean>

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.

Entity1.java

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;

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

private String name;

@Type(type="onezero-boolean")
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.

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

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.

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 http://code.google.com/p/gwt-image-loader/. 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 (http://code.google.com/p/gwt-image-loader/). It's free and open source.

GTUG3 Agenda

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

Gtug

Gtug Jay Williams

Gtug

Gtug

Gtug

Google Technology meeting on Wednesday, Aug. 12, 2009

Agenda:


Events

Google App Engine: Too Many Versions (403) error

I got below error after deploying my application to app engine server 12 times:

Too Many Versions (403)

The application already has the maximum number of versions.

Solution:

Manually delete previous outdated versions of your application on App Engine with caution. Deleting previous versions of deployed applications allow space for deploying new versions on Google App Engine.

Special Character Cheat Sheet

Here is a useful cheat sheet for keyboard shortcuts and HTML code for some of the common special characters:

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)

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);
}
}

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.







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.

http://ukitech.blogspot.com/2009/07/test-your-internet-speed-in-chicago.html


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).

References:

iPhone used for new Nissan car remote

http://www.infoworld.com/d/mobilize/nissan-dials-iphone-car-remote-control-768

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):


















Open:



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






public UserDao()
{
init();
}

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

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













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