Home > Hibernate

Example of @MapKeyColumn in Hibernate

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 states;
 
}


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

	@Column(name = "name")
	private String name;
}	
 

comments powered by Disqus


Home    Core Java    Spring    Hibernate    Jquery    My Computer    News    Forum    Quiz    Interview Q & A     Maven     Eclipse IDE

Copyright ©2014 concretepage.com | All rights reserved |Privacy