Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aalexandrov/6973114 to your computer and use it in GitHub Desktop.
Save aalexandrov/6973114 to your computer and use it in GitHub Desktop.
@Entity
@Table(name = "users")
public class User {
@Id
@Column(nullable = false, length = 50)
private String username;
@Column(nullable = false, length = 50)
private String password;
@Column(nullable = false)
private boolean enabled = true;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Authority> authorities;
....
}
public class AuthorityId implements Serializable {
private static final long serialVersionUID = -4251643133848097809L;
String user;
String authority;
@Override
public int hashCode() {
int hash = 23;
hash = hash * this.user.hashCode();
hash = hash * 31 + this.authority.hashCode();
return hash;
}
@Override
public boolean equals(Object obj) {
AuthorityId x = (AuthorityId) obj;
return (x.user.equals(this.user)) && (x.authority.equals(this.authority));
}
}
@Entity
@Table(name = "authorities")
@IdClass(AuthorityId.class)
public class Authority {
@Id
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "username", nullable = false)
private User user;
@Id
@Column(nullable = false, length = 50)
private String authority;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment