I am trying to create the following tables for getting user information when a user is registering.
From the tutorials that i have read over the internet i need to create four entity classes namely USER,USER_DETAILS,ROLE and USER_ROLE which i have done. However my problem is when i want to create a user in my application do i have to have 4 different post methods or is there a way i can add the user data in one go to the relevant tables.
I would like to be able to post data to a single url and all these table with there foreign keys be filled at once when a user gives all the details.
Something like :
{
"userName": “ME",
"password": "1234”,
“DOB": “1988-05-12",
"email": “test@test.org",
“role": “admin",
"country": "Ireland",
"registrationLocation": "Dublin",
"timeStamp": "18-01-2018T12:23:08"
}
posting it to a single url such as : localhost:8080/live/api
This is my code:
UserDetails
public class UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private Users user;
@NotBlank
private String DOB;
public UserDetails() {
}
public UserDetails( String DOB) {
this. DOB = DOB;
}
public String getDOB() {
return DOB;
}
public void setTitle(String DOB) {
DOB = DOB;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "userID")
public Users getUser() {
return user;
}
User
@Entity
@Table(name = "Users",
uniqueConstraints =
{
@UniqueConstraint(columnNames = "email"),
@UniqueConstraint(columnNames = "username")
}
)
public class Users implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long userID;
@NotBlank
@Size(min = 5, max = 100, message = "Please enter between 8 and 100 characters.")
private String password;
@NotBlank
private String username;
private Role role;
public Users() {
}
public Users(long userID, String password, String username, Role role) {
this.userID = userID;
this.password = password;
this.username = username;
this.role = role;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "roleID")
public Role getRoles() {
return role;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
UserRole
@Entity
public class UserRole implements Serializable {
private Role role;
private Users user;
public UserRole() {
}
public UserRole(Role role, Users user) {
this.role = role;
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
}
Controller class method
@RequestMapping(method = RequestMethod.POST, value = "/api/usermanagement/users")
public Response createUser(@Valid @RequestBody Users user){
return userService.AddUser(user);
}
You can post all the relevant information to 1 endpoint like you have given an example and then from UserService class (an Impl class preferably) you can call all the relevant methods to save the relevant details. For example,
@Service
public class UserServiceImpl implements UserService {
public Response addUser(Users user){
//get details from Users and populate to entities User and UserDetails.
// Save User and UserDetails. Return Response
}
}
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.