Created
August 7, 2012 16:55
-
-
Save bmchild/3287283 to your computer and use it in GitHub Desktop.
Foreign Key to a Compsite Primary Key in Hibernate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2177 | |
*/ | |
@Entity | |
public class Parent implements Serializable { | |
@Id | |
public ParentPk id; | |
public int age; | |
@OneToMany(cascade=CascadeType.ALL) | |
@JoinColumns ({ | |
@JoinColumn(name="parentCivility", referencedColumnName = "isMale"), | |
@JoinColumn(name="parentLastName", referencedColumnName = "lastName"), | |
@JoinColumn(name="parentFirstName", referencedColumnName = "firstName") | |
}) | |
public Set<Child> children; //unidirectional | |
... | |
} | |
@Entity | |
public class Child implements Serializable { | |
@Id @GeneratedValue | |
public Integer id; | |
@ManyToOne | |
@JoinColumns ({ | |
@JoinColumn(name="parentCivility", referencedColumnName = "isMale"), | |
@JoinColumn(name="parentLastName", referencedColumnName = "lastName"), | |
@JoinColumn(name="parentFirstName", referencedColumnName = "firstName") | |
}) | |
public Parent parent; //unidirectional | |
} | |
@Embeddable | |
public class ParentPk implements Serializable { | |
String firstName; | |
String lastName; | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment