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!
Advertisement
Categories: Java, Programming
annotation, enumeration, hibernate, Java
Great article… Keep on share ya!