Example of @MapKey in Hibernate

By Arvind Rai, May 23, 2013
In hibernate, @MapKey is used when the entity is returning Map. Map is key value pair. So to decide what should be the key of map, @MapKey is used. In our example Country entity is returning the Map of states and here the key of map has been set to id of sate by @MapKey. The syntax is:
 @MapKey(name="column name")
 


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.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKey;
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")
	@MapKey(name="id")
	private Map<Integer,State> states;
	
	public Country(int id,String name,Map<Integer,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<Integer,State> getStates() {
		return states;
	}
	public void setStates(Map<Integer,State> states) {
		this.states = states;
	}
}
 


State.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "state")
public class State {
	@Id
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;
		
	public State(int id,int countryId,String name){
		this.id=id;
		this.name=name;
	}
	public State(){}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}
 


 package com.concretepage.util;
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();
	    	Country c=(Country)session.get(Country.class, new Integer(1));
	    	Map<Integer,State> states = c.getStates();
	    	Iterator entries = states.entrySet().iterator();
	    	while (entries.hasNext()) {
	    	    Map.Entry entry = (Map.Entry) entries.next();
	    	    Integer key = (Integer)entry.getKey();
	    	    State value = (State)entry.getValue();
	    	    System.out.println("Key = " + key + ", Value = " + value.getName());
	    	}
	    	session.close();
	   }
	}
 
Data in database is :

Example of @MapKey  in Hibernate

Output
Key = 1, Value = UP
Key = 2, Value = MP
Key = 3, Value = HP
Key = 4, Value = DELHI
 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us