Skip to content

Instantly share code, notes, and snippets.

@mohamed-gara
Created July 13, 2018 05:53
Show Gist options
  • Save mohamed-gara/cb6c8b1160abe62b6c022ced07234d75 to your computer and use it in GitHub Desktop.
Save mohamed-gara/cb6c8b1160abe62b6c022ced07234d75 to your computer and use it in GitHub Desktop.
Person with partner relationship
@Entity
@Data
public class Person extends core.Person {
@OneToOne(fetch = LAZY)
private Person wife;
@OneToOne(fetch = LAZY, mappedBy = "wife")
private Person husband;
public Person getPartner() {
if ((wife != null) && (husband != null)) throw new IllegalStateException();
if (wife != null) return wife;
if (husband != null) return husband;
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment