Is it possible in hibernate
to model a use case in which you have two fields and either of them could be null but at least one of them must not be null? Below is the code I have at the moment but I don't like the fact that I have to set both of them to @Column(nullable = true)
. In my case I want either the personal email address or the work address. Is there a good way to support this? Or is an alternative approach needed?
public class ApplicantDetails {
//...
@OneToOne(optional = false)
private ContactDetails contactDetails;
//...
}
public class ContactDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
/*Need at least one email address but it doesn't matter which one.*/
@Column(nullable = true, unique = true)
private String personalEmail;
@Column(nullable = true, unique = true)
private String workEmail;
//...
}