如果我只有一个注册表单,如何将数据插入到2个表中 [英] How do I insert data into 2 tables if I have just one registration form

查看:90
本文介绍了如果我只有一个注册表单,如何将数据插入到2个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:

< pre lang =java> 
public class User实现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>();
// setter and getters







 < span class =code-keyword> public   class 角色 implements  Serializable {

@ Id
@ GeneratedValue (strategy = GenerationType.AUTO)
private Long id;

@ Column (name = userRole
private String userRole = ROLE_STUDENT;

@ ManyToOne
@ JoinColumn (name = user_id
private 用户用户;
// setter and getters





每位用户都可以是学生或老师。



和注册表格:

< pre lang =HTML> < sf:form class = registerForm 方法 = POST modelAttribute = user >
< div cl ass = top-margin form-group >
< label > 注册为< / label >
< sf:select id = input_loginas name = input_loginas class = form-control >
< 选项 value = > 选择一个选项... < / option >
< 选项 value = 学生 > 学生< / option >
< 选项 value = 老师 > 老师< / option >
< / sf:select >
< / div >
< div class = top-margin form-group >
< label > 名字< / label >
< sf:input path = firstName type = text name < span class =code-keyword> = firstName id = firstName class = 表单控制 >
< / sf:input >
< / div >



角色应该是学生或老师。

如果用户从下拉列表中选择一个选项,我想在角色表中插入他的角色,并在用户表中插入其余的细节和我不知道为什么因为2个表。(我想在spring security中使用这些角色)

解决方案

使用存储过程。

使用参数发送数据,然后插入两个查询。


我不知道如何写这个..我尝试了不同的查询,但没有人工作。


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

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:

<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)

解决方案

Use a stored procedure.
Send data using parameters and then insert both the queries.


I don't know how to write this ..I tried different queries but no one worked.


这篇关于如果我只有一个注册表单,如何将数据插入到2个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆