I currently have a User
entity for my users and an Authority
entity for my user "roles". This works well if my application only required users to have a specific authority
to get tasks done, but that is not the case. Before I move on, here is the code for those entities:
User:
@Data
@Entity
@Table(name = "users")
public class User {
@JsonIgnore
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
@SequenceGenerator(name = "user_seq", sequenceName = "user_seq", allocationSize = 1)
private Long id;
@Column(name = "username" unique = true)
@NotNull
@Size(min = 4, max = 50)
private String username;
@JsonIgnore
@Column(name = "password")
@NotNull
private String password;
@Column(name = "email", unique = true)
@NotNull
private String email;
@JsonIgnore
@Column(name = "activated")
@NotNull
private boolean activated;
@ManyToMany
@JoinTable(
name = "user_authorities",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")}
)
private Set<Authority> authorities = new HashSet<>();
}
Authority:
@Data
@Entity
@Table(name = "authorities")
public class Authority {
@Id
@Column(name = "name")
@NotNull
private String name;
}
Now, if I pull a user, I get something like this:
{
username: "john",
email: "john@doe.com",
authorities: [
{
name: "ADMIN"
}
]
}
Now, I am adding Company
and Location
entities into the mix. I would like a user to be able to have, let's say, ADMIN
role for one Company
, USER
role for a certain Location
, and a normal USER
role. So for this said user, I would like something like this:
{
username: "john",
email: "john@doe.com",
authorities: [
{
name: "ADMIN",
companyId: 3
},
{
name: "USER",
locationId: 12
},
{
name: "USER"
}
]
}
I was thinking of just adding 2 NULLABLE
columns to my joinTable
, company_id
and location_id
, but I can't seem to add more than one join to the user_authorities
table since it says that the table is already being joined with another entity. Is there a simple way to add this to my current setup or am I going about it the wrong way? Any enlightenment on this issue will be greatly appreciated. Thank you!
You can create other entity user_authorities
and create two bidirecional @OneToMany
associations.. Ex:
@Data
@Entity
@Table(name = "user_authorities")
public class UserAuthority {
@JsonIgnore
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_authorities_seq")
@SequenceGenerator(name = "user_authorities_seq", sequenceName = "user_authorities_seq", allocationSize = 1)
private Long id;
@ManyToOne
@JoinColumn(name="user_id", nullable=false)
private User user;
@ManyToOne
@JoinColumn(name="authority_id", nullable=false)
private Authority authority;
@NotNull
private String otherField;
@NotNull
private String otherField2;
//...
public UserAuthority() {}
// getters and setters
Replace the @ManyToMany
association with two bidirectional @OneToMany
associations, so create @Entity
for user_authorities table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.