I have 2 tables:
<pre lang="java">
public class User implements Serializable {
private static final int MIN_SIZE = 3;
private static final int MAX_SIZE = 50;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty
@Column(name = "first_name", nullable = false)
@Size(min = MIN_SIZE, max = MAX_SIZE)
private String firstName;
@NotEmpty
@Column(name = "last_name", nullable = false)
@Size(min = MIN_SIZE, max = MAX_SIZE)
private String lastName;
@Email
@Column(name = "email")
private String email;
@NotEmpty
@Column(name = "password")
private String password;
@Column(name = "confirm_password")
private String confirmPassword;
@Column(name = "year_degree")
private int yearDegree;
@Column(name = "degree")
private String degree;
@Column(name = "domain")
private String domain;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private Set<Theme> themeList = new TreeSet<Theme>();
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private Set<Enroll> enrollList = new TreeSet<Enroll>();
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private Set<Role> userRoles = new TreeSet<Role>();
and
public class Role implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name="userRole")
private String userRole="ROLE_STUDENT";
@ManyToOne
@JoinColumn(name="user_id")
private User user;
Each user can be a student or a teacher.
And a register form:
<sf:form class="registerForm" method="POST" modelAttribute="user">
<div class="top-margin form-group">
<label>Register as a</label>
<sf:select id="input_loginas" name="input_loginas" class="form-control">
<option value="">Choose an option...</option>
<option value="student">student</option>
<option value="teacher">teacher</option>
</sf:select>
</div>
<div class="top-margin form-group">
<label>First Name</label>
<sf:input path="firstName" type="text" name="firstName" id="firstName" class="form-control">
</sf:input>
</div>
The roles should be student or teacher.
If an user selects an option from the dropdown list, I want to insert in role table his role and in the user table the rest of the details and I don't know how because are 2 tables.(I want to use these roles in spring security)