Home  >  Hibernate

Example of @MapKeyJoinColumn in Hibernate

By Arvind Rai, May 26, 2013
In Hibernate, @MapKeyJoinColumn can be used when the key of map is an object. The key as an entity can be set by configuring @MapKeyJoinColumn as below
@MapKeyJoinColumn( name="id")
 


Country.java
@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")
	@MapKeyJoinColumn( name="id")
	private Map<Name,State> states;
} 


Name.java
@Entity
@Table(name="name")
public class Name implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name="id")
	private int id;
	@Column(name="name")
	private String name;
} 


State.java
@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;
} 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us