package example.entity; import javax.persistence.*; import java.util.List; import java.util.ArrayList; import java.util.Collections; @Entity public class UserCredential implements java.io.Serializable { @Id // name is the PK of this entity. private String name; private String password; @OneToMany(mappedBy = "user", cascade=CascadeType.ALL) @OrderBy("loginTime DESC") private List loginAttempts = new ArrayList(); protected UserCredential() { // Every entity bean must have a default public or protected constructor } public UserCredential(String name, String password) { if ( (name == null || name.length() == 0) || (password == null || password.length() == 0)) { throw new IllegalArgumentException("name or password is empty"); } this.name = name; this.password = password; } public String getName() { return name; } public boolean isMatchingPassword(String password) { return this.password.equals(password); } public List getLoginAttempts() { return Collections.unmodifiableList(loginAttempts); } public void addLoginAttempt(LoginAttempt attempt) { loginAttempts.add(attempt); } }