Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>();
//setters and getters


and
Java
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;
//setters and getters


Each user can be a student or a teacher.

And a register form:
HTML
<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)
Posted

Use a stored procedure.
Send data using parameters and then insert both the queries.
 
Share this answer
 
I don't know how to write this ..I tried different queries but no one worked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900