Home  >  Hibernate

Example of @Enumerated in Hibernate

By Arvind Rai, May 12, 2013
@Enumerated defines how the value should be saved in database. @Enumerated has two types. EnumType.STRING and EnumType.ORDINAL are two type of @Enumerated in hibernate.

User.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")  
public class User {
	@Id
	private int id; 
	
	@Enumerated(EnumType.STRING)
	@Column(name="gender")  
	private  Gender gender;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
} 
 


Gender.java
 package com.concretepage.persistence;
enum Gender {  
    MALE, FEMALE;  
    public static String getGenderName(Gender gen){  
	  switch (gen){  
	   case MALE : return "Man";  
	   case FEMALE : return "Woman";  
	   default : return null;  
	  }  
	 }  
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us