Enumeration Mapping in Hibernate
In this simple tutorial i will show you, how to map a Java 5 Enumeration (enum) to a varchar column in Hibernate. The use-case that i'm going to demonstrate is very simple, I have a User class which has a reference to an Enumeration Gender.
Gender Enumeration
package com.javachap.domain.user;
/**
* Male or Female. Used to customize Account messages (e.g. he or she usage) and
* to choose the default profile picture.
*/
public enum Gender {
Male, Female;
}
User Domain:
package com.javachap.domain.user;
public class User {
private String firstName;
private String lastName;
private Gender gender;
private String email
/**
* Gets the <code>User</code>'s firstName
*
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the <code>User</code>'s firstName
*
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets the <code>User</code>'s lastName
*
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* Sets the <code>User</code>'s lastName
*
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets the <code>User</code>'s email
*
* @return the email
*/
public String getEmail() {
return email;
}
/**
* Sets the <code>User</code>'s email
*
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
}
Hibernate Mapping (HBM):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class table="user" name="com.javachap.domain.user.User">
<id name="uid" column="usr_uid">
<generator class="increment"/>
</id>
<property name="firstName" type="java.lang.String" column="usr_first_name"/>
<property name="lastName" type="java.lang.String" column="usr_last_name" />
<property name="gender" column="usr_gender" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">com.javaachap.domain.user.Gender</param>
<param name="type">12</param>
</type>
</property>
<property name="email" type="java.lang.String" column="usr_email"/>
</class>
</hibernate-mapping>
By default Hibernate persists the enumeration value as integer in the database when type param is not specified, In order to store the enum display name, we use a param type, which should be pointed to value in java.sql.Types. Since we want to store the enum display name in database we have mapped it to 12 which is equivalent to java.sql.Types.VARCHAR.
Hibernate Annotation Based Mapping:
If you prefer convention over configuration, below is the way to map an enum using annotation.
public class User {
@Enumerated(EnumType.STRING)
Gender gender
}
Java 7′s Fork/Join Framework
Eric Bruno:
Fork/Join is an enhancement to the
ExecutorServiceimplementation that allows you to more easily break up processing to be executed concurrently, and recursively, with little effort on your part. It's based on the work of Doug Lea, a thought leader on Java concurrency, at SUNY Oswego. Fork/Join deals with the threading hassles; you just indicate to the framework which portions of the work can be broken apart and handled recursively. It employs adivide and conquer algorithm that works like this in pseudocode (as taken from Doug Lea's paper on the subject):
Understanding Java Weak References
Ethan Nicholas :
I'm sure some of you are grumbling by now, as I'm talking about an API which is nearly a decade old and haven't said anything which hasn't been said before. While that's certainly true, in my experience many Java programmers really don't know very much (if anything) about weak references, and I felt that a refresher course was needed. Hopefully you at least learned a little something from this review.
A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself.
http://weblogs.java.net/blog/2006/05/04/understanding-weak-references