My entity class MemberDispatch has 2 natural keys: Members and Dispatch.
Both Members and Dispatch are entity classes themselves.
Right now, the key of MemberDispatch is ID-- a surrogate key.
Members and Dispatch are composed in MemberDispatch as entity fields
and are not part of the key.
Whenever I need to save-and-not-update, i.e., save only if a corresponding
(Members, Dispatch) doesn't exist in MemberDispatch, I first am
fetching from MemberDispatch (if exists)
the record matching the (Members, Dispatch) pair, and saving
ony if that query returns null.
If I'd instead define and use the natural keys (Members and Dispatch)
as the key, i'd save directly without checking first a record already exists. With this,
I'd have achieved what I'm looking for--
Hibernate would make sure of the uniqueness of (Members and Dispatch) pair in the database table.
I've seen Why are composite keys discouraged in hibernate? and some other arguments against composite keys in Hibernate.
My Q is: is there a better way of designing the entity classes without getting into composite keys?
//-------------------------
EDIT:
I achieved since by overriding equals() of MemberDispatch on fields Members and Dispatch. it's throwing a org.hibernate.exception.ConstraintViolationException when I attempt to save an existing (Members, Dispatch) pair.
The Q then would be - why would I need JPA uniqueness constraints (thanks JBNizet and Nathan Hughes) while I have this?