Enumeration in Hibernate with Annotation


I tried on Google for this keyword, and Google returns lots of links on how to implement this, one at the topmost is about writing a helping class called EnumUserType.

This solution is outdated as new versions of Hibernate 3 support JPA annotation @Enumerated. Using this former method is much simpler and therefore recommended.

Using @Enumerated annotation as below:

Filename: Pengiriman.java

public enum Pengiriman {

	/**
	 * Langsung
	 */
	L,
	/**
	 * Biro Umum
	 */
	B;

}

Filename: Surat.java

public class Surat extends CoreDomain {

	private static final long serialVersionUID = -7531117940439190324L;

	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	@Column(name = "ID", unique = true, nullable = false, length = 32)
	private String id;

	@Enumerated(EnumType.STRING)
	@Column(name = "PENGIRIMAN", length = 1)
	private Pengiriman pengiriman;

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Pengiriman getPengiriman() {
		return this.pengiriman;
	}

	public void setPengiriman(Pengiriman pengiriman) {
		this.pengiriman = pengiriman;
	}
}

I use @Enumerated(EnumType.STRING) in the attribute because I want to store attribute PENGIRIMAN as String in database.
And that’s it!

One thought on “Enumeration in Hibernate with Annotation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s