Example of @MapKeyEnumerated in Hibernate
May 25, 2013
@MapKeyEnumerated is used when map key type is enum. We need to pass EnumType.ORDINAL or EnumType.STRING. When we fetch the value, the map key will be Enum.
It is annotated a
@MapKeyEnumerated(EnumType.ORDINAL)
Country.java
package com.concretepage.persistence; import java.io.Serializable; import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.MapKeyEnumerated; import javax.persistence.OneToMany; @Entity public class Country implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="id") private int id; @Column(name="name") private String name; @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="country_id") @MapKeyEnumerated(EnumType.ORDINAL) private Map<Area,State> states; public Country(int id,String name,Map<Area,State> states){ this.id=id; this.name=name; this.states=states; } public Country(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<Area,State> getStates() { return states; } public void setStates(Map<Area,State> states) { this.states = states; } }
State.java
package com.concretepage.persistence; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "state") public class State implements Serializable{ private static final long serialVersionUID = 1L; @Id @Column(name = "id") private int id; @Id @Column(name = "country_id") private int countryId; @Column(name = "area") private Area area; @Column(name = "name") private String name; public State(int id,int countryId,Area area,String name){ this.id=id; this.countryId=countryId; this.area=area; this.name= name; } public State(){} public Area getArea() { return area; } public void setArea(Area area) { this.area = area; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getCountryId() { return countryId; } public void setCountryId(int countryId) { this.countryId = countryId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Area.java
package com.concretepage.persistence; public enum Area { SMALL(0), MEDIUM(1), LARGE(2); private int code; private Area(int code) { this.code = code; } public int getStatusCode() { return code; } }
Output
package com.concretepage.util; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.concretepage.persistence.Area; import com.concretepage.persistence.Country; import com.concretepage.persistence.State; public class HibernateUtil { private static final SessionFactory concreteSessionFactory; static { try { Properties prop= new Properties(); prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate"); prop.setProperty("hibernate.connection.username", "root"); prop.setProperty("hibernate.connection.password", ""); prop.setProperty("hibernate.hbm2ddl.auto", "update"); prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect"); concreteSessionFactory = new AnnotationConfiguration() .addPackage("com.concretepage.persistence") .addProperties(prop) .addAnnotatedClass(Country.class) .addAnnotatedClass(State.class) .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return concreteSessionFactory.openSession(); } public static void main(String... args){ Session session=getSession(); session.beginTransaction(); State s1= new State(1,1,Area.LARGE,"UP"); State s2= new State(2,1,Area.MEDIUM,"MP"); State s3= new State(3,1,Area.SMALL,"HP"); Map<Area, State> map= new HashMap<Area,State>(); map.put(Area.LARGE, s1); map.put(Area.MEDIUM, s2); map.put(Area.SMALL, s3); Country c= new Country(1, "India", map); session.persist(c); session.getTransaction().commit(); Country cval=(Country)session.get(Country.class, new Integer(1)); Map<Area,State> states = cval.getStates(); Iterator entries = states.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); Area key = (Area)entry.getKey(); State value = (State)entry.getValue(); System.out.println("Key = " + key + ", Value = " + value.getName()); } session.close(); } }
Output
Key = LARGE, Value = UP Key = MEDIUM, Value = MP Key = SMALL, Value = HP