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.



As an Amazon Associate I earn from qualifying purchases.

OurSportsCommunity.com: Kenosha, WI



As an Amazon Associate I earn from qualifying purchases.

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



As an Amazon Associate I earn from qualifying purchases.

Test



As an Amazon Associate I earn from qualifying purchases.

Ксения Симонова - Kseniya Simonova - Sand Animation

This is so beautiful and moving...



As an Amazon Associate I earn from qualifying purchases.

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.


As an Amazon Associate I earn from qualifying purchases.

Buns



As an Amazon Associate I earn from qualifying purchases.

Dream about Liliann

I had a dream about Liliann last night, I was on the crowded bus going downhill in my home town and Liliann was on a small bike trying to catch up. On the downhill she was going fast next to the bus, calm on her trooper face, but pedaling very fast, the problem is she was not good about stopping the bike. I was yelling to her to stop and wait because I would come and find her where she was (I was my home town, so I knew the streets). She did stop eventually and I woke up.


As an Amazon Associate I earn from qualifying purchases.

Healthy stuff

With so few things going "healthy" in my life now, at least the food
has improved, but as Igor (my cousin) wrote today, it sucks to eat
alone.

I got a blender yesterday: banana milk shake with a couple cherries
and much sugar baby!!! Ha ha, sounds bad, taste awesome.



As an Amazon Associate I earn from qualifying purchases.

Let us pray for ....


EMBED-One Indian, One Normal - Watch more free videos


As an Amazon Associate I earn from qualifying purchases.

Flowers



As an Amazon Associate I earn from qualifying purchases.

Flower



As an Amazon Associate I earn from qualifying purchases.

Farmers' cheese buns - made from scratch

Before baking...



















After baking... yumy!


As an Amazon Associate I earn from qualifying purchases.

mysterium

I woke up today remembering a dream about my friend Kajtek, I opened my email and there was a message from him. You may say it is a coincidence, but we have not seen each other in years and write only sporadically.

I've read his blog one more time and I found so many "pearls" that I wanted to share in my so-imperfect English translation... he'd so eloquently wrote about a trip to the mountains:

"The `mysterium` begins.
In case you don't understand, step-by-step march forward.
[...] Rain. The views do not disturb, neither the conversations. Everyone in the thoughts under their own rain coat during which everyone is getting closer to themselves, renews the friendship with oneself, cut off from all the assaults of civilization. Visibility: 2 meters, we are resting under the rock outcropping with some other tourists. Our faces are smiling, as if we followed the motto: `the worse conditions, the better the joy`. I can feel the closeness between fellow humans, even if we are just standing there for a moment, I have not felt this unity in any church before. To feel it you have to get closer, make an effort, mere fact of getting out will not do. At the shelter there are tourists in flip-flops who took the ski-lift on the way up, I noticed the road construction equipment to flatten the road... what more can I say.
[...] The evening in the shelter on the Czech side has a wonderful climate, we are lucky, there are no `forced` tourist groups, brought from the cities against their own will. The quiet conversations over the beer, tea and coffee. Someone is wondering around the shelter, some other group is taking pictures of the long shadows. One man cast his gaze upon the ceiling in lazy meditation and there is no sign that he will let it go anytime soon..."





As an Amazon Associate I earn from qualifying purchases.

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.


As an Amazon Associate I earn from qualifying purchases.

Freshly baked buns with various filling



As an Amazon Associate I earn from qualifying purchases.

GTUG3 Agenda

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



As an Amazon Associate I earn from qualifying purchases.

Gtug



As an Amazon Associate I earn from qualifying purchases.

Gtug Jay Williams



As an Amazon Associate I earn from qualifying purchases.

Gtug



As an Amazon Associate I earn from qualifying purchases.

Gtug



As an Amazon Associate I earn from qualifying purchases.

Gtug



As an Amazon Associate I earn from qualifying purchases.

Google Technology meeting on Wednesday, Aug. 12, 2009

Agenda:


Events


As an Amazon Associate I earn from qualifying purchases.

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.



As an Amazon Associate I earn from qualifying purchases.

Lili



As an Amazon Associate I earn from qualifying purchases.

Special Character Cheat Sheet

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


As an Amazon Associate I earn from qualifying purchases.

Another attempt at baking bread



As an Amazon Associate I earn from qualifying purchases.

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)


As an Amazon Associate I earn from qualifying purchases.

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



As an Amazon Associate I earn from qualifying purchases.

St. Mary, Mundelein

I am running at
http://maps.google.com/maps?q=42.280514,+-87.999650

Pictures later...



As an Amazon Associate I earn from qualifying purchases.

Nikon D40 photos



Since I have Nikon D40, I like to look at what kind of awesome pictures others take with it....


http://www.flickr.com/cameras/nikon/d40/










As an Amazon Associate I earn from qualifying purchases.

Kenosha, WI

I love Kenosha lakefront; 
such a nice place to go out for lunch, 
but I also feel so isolated from the world there 
-- no amount of pretty replaces being with friends.

I was looking forward to go to Milwaukee tomorrow, 

too bad that fell thru.



As an Amazon Associate I earn from qualifying purchases.

Entrepreneurial Thought Leaders






As an Amazon Associate I earn from qualifying purchases.

Podcast: eHarmony

I think this is a really worthwhile podcast.






As an Amazon Associate I earn from qualifying purchases.

Podcast: eHarmony

I think this is a really worthwhile podcast.






As an Amazon Associate I earn from qualifying purchases.

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.









As an Amazon Associate I earn from qualifying purchases.

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.



As an Amazon Associate I earn from qualifying purchases.

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:


As an Amazon Associate I earn from qualifying purchases.

iPhone used for new Nissan car remote

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


As an Amazon Associate I earn from qualifying purchases.

Cheese buns baked, try #1



As an Amazon Associate I earn from qualifying purchases.

Vanilla farmer's cheese yeast buns

Night before baking.


As an Amazon Associate I earn from qualifying purchases.

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.


As an Amazon Associate I earn from qualifying purchases.

Hafez' verse




Even though alone, I decided I shall "taste" life to its fullest.

It is nice to visit Quebec or Europe, but... 
home is where the heart is, and if we do not have the heart to feel at home where we are now, 
we will never be at peace.

I made apricot-and-date buns, good bergamot tea, and Shiraz wine and wrote matching poetry, oil lamp light... Bon appetite!




Shiraz.. she left tonight her ruby mark
My night's companion after dark
A crecent stamp on Hafez' verse
Of lover's longing well rehearsed
I dipped my lips in thoughts of love
soul-mates tonight like wings of a dove
Cross distance fast and have you near
My mind is calm, goodnight my dear.


As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles