@MapKeyTemporal in Hibernate
May 26, 2013
Hibernate @MapKeyTemporal is used when map key is date. Date is mapped while inserting data in the table and while fetching the same, date will be key for the map. @MapKeyTemporal is used as below.
@MapKeyTemporal(TemporalType.TIMESTAMP)
Find the example.
Country.java
package com.concretepage.persistence; import java.io.Serializable; import java.util.Calendar; import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.MapKeyTemporal; import javax.persistence.OneToMany; import javax.persistence.TemporalType; @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") @MapKeyTemporal(TemporalType.TIMESTAMP) private Map<Calendar,State> states; public Country(int id,String name,Map<Calendar,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<Calendar,State> getStates() { return states; } public void setStates(Map<Calendar,State> states) { this.states = states; } }
State.java
package com.concretepage.persistence; import java.io.Serializable; import java.util.Calendar; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.MapKeyTemporal; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @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 = "name") private String name; @Temporal(TemporalType.TIMESTAMP) @Column(name="create_date") private Calendar createDate; public State(int id,int countryId,String name,Calendar createDate){ this.id=id; this.countryId=countryId; this.name= name; this.createDate=createDate; } public State(){} 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; } }
package com.concretepage.util; import java.util.Calendar; 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.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(); Calendar calendar = Calendar.getInstance(); Map<Calendar, State> map= new HashMap<Calendar,State>(); calendar.add(Calendar.YEAR,-50); State s1= new State(1,1,"UP",calendar); map.put(calendar, s1); calendar.add(Calendar.DAY_OF_MONTH,-10); State s2= new State(2,1,"MP",calendar); map.put(calendar, s2); calendar.add(Calendar.DAY_OF_MONTH,-10); State s3= new State(3,1,"HP",calendar); map.put(calendar, 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<Calendar,State> states = cval.getStates(); Iterator entries = states.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); Calendar key = (Calendar)entry.getKey(); State value = (State)entry.getValue(); System.out.println("Key = " + key.getTimeInMillis() + ", Value = " + value.getName()); } session.close(); } }