Last active
November 2, 2021 18:42
-
-
Save aalexandrov/6973114 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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