Instead of a boolean
why not add a (possibly another, since you seem to track everyone who had a key) @ManyToOne reference to your person entity to the AccessCard Entity?
Like this:
public class AccessCard { @ManyToOne(Cascade = CascadeType.PERSIST | CascadeType.MERGE) @JoinColumn(referencedColumnName="person_id") private Person cardHolder; public getCardHolder() { return cardHolder; } } public class Person { @OneToMany(Cascade = CascadeType.PERSIST | CascadeType.MERGE) private Set posessingCards; }
- Now you can check if a specific card is in use by checking for
null
on the return ofgetCardHolder
. Or you could just add another method the does the check and returns a boolean instead. - This would ensure that transferring the card to a new person removes it from the current one and a check to see if it's active would just be a check for null instead.
- Activation/Deactivation would effectively be handled by assigning the key to a person, two birds with one stone, you know if it's active AND who has it.