«

»

Jan
26

Hibernate Enum Datatype Mapping

If you want to map a enum in JPA/Hibernate POJO class, so that only specific values can be saved for underlying database column, it is possible now very easily.

public enum StatusEnum {
ACTIVE,
IDLE,
PENDING,
APPROVED;
}

The sample POJO File is given below, have an idea. @Enumerated( javax.persistence.EnumType.STRING ) tells what values you want into database from provided enum type, & hibernate will automatically convert that enum into that specified data type.


@Entity
@Table( name = "Invoice" )
public class Testtable implements java.io.Serializable {

	// Fields

	private Integer		id;
	private StatusEnum		status;

	@Column( name = "status", length = 64 , columnDefinition = "enum(active,pending,idle,approved)")
	@Enumerated( javax.persistence.EnumType.STRING )
	public StatusEnum getStatus( ) {
		return status;
	}

	public void setStatus( StatusEnum status ) {

		this.status = status;
	}

	// Property accessors
	@Id
	@Column( name = "id", unique = true, nullable = false )
	public Integer getId( ) {

		return this.id;
	}

	public void setId( Integer id ) {

		this.id = id;
	}
}
VN:F [1.9.14_1148]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.14_1148]
Rating: 0 (from 0 votes)

About the author

Faisal Basra

Faisal Basra is an independent consultant, software developer, writer, blogger, speaker, architect and technology leader in Lahore, Pakistan. He has been a professional software developer since 2008, has been writing code since 2006.Having hands on experience of popular Java EE frameworks & technologies like JSF, Spring, Hibernate, Enverse, JPA, Richfaces, Primefaces, JSP/Servlet.Tools & Servers: jUnit, Log4j, Maven, Eclipse, MyEclipse, NetBeans, Tomcat, Jboss, WebLogicMobile Development: Google Android

Permanent link to this article: http://www.javaplex.com/blog/hibernate-enum-datatype-mapping/

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>