Example of @MapKeyColumn in Hibernate

By Arvind Rai, May 20, 2013
@MapKeyColumn is used when map key is basic type. @MapKeyColumn is used for index in a Map. In Map value will be the any entity object and index will be the value defined by @MapKeyColumn. The syntax to use @MapKeyColumn is
 @MapKeyColumn(name="id")
 
If we have two entities as States and Country, we will use it as below.
@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")
	@MapKeyColumn(name="id")
	private Map<Integer,State> states;
 
}
@Entity
@Table(name = "state")
public class State {
	@Id
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;
}	
 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us