Example of @ForeignKey in Hibernate

By Arvind Rai, May 30, 2013
Hibernate allows to keep foreign key name. Hibernate overrides the foreign key name by @ForeignKey. It has the attribute name that should be defined. Find the example.

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

	@Column(name = "name")
	private String name;
	
	@ManyToOne
	@ForeignKey(name="FK_COUNTRY")
	private Country country;
}
 


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")
	private Set<State> states;
}
 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us