我在数据库中有一个表:
UserRoles(字典)
UsersLogins
的UserProfiles
用户
如何将值插入上述三个表?
我试过这样的事情:
控制器:
@Autowired
private UserProfilesService userProfilesService;
@Autowired
private D_UserRolesService d_UserRolesService;
@Autowired
private UserLoginsService loginsService;
@Autowired
private UsersService usersService;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAddNewUserForm(Model model) {
UserProfiles userProfiles = new UserProfiles();
UserLogins userLogins = new UserLogins();
Users users = new Users();
model.addAttribute("userProfiles", userProfiles);
model.addAttribute("users", users);
model.addAttribute("userLogin", userLogins);
model.addAttribute("userRoless", d_UserRolesService.getAllRoles());
return "userNew";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String processAddNewUserForm(@ModelAttribute("userProfiles") UserProfiles profileToBeAdded,
@ModelAttribute("userLogin") UserLogins loginToBeAdded, @ModelAttribute("users") Users userToBeAdded,
HttpServletRequest request) {
userProfilesService.addUser(profileToBeAdded);
loginsService.AddOrUpdateUser(loginToBeAdded);
usersService.AddOrUpdateUser(userToBeAdded);
return "redirect:/users";
}
JSP PAGE:
<form:form class="form-horizontal" >
<fieldset>
<legend>New user</legend>
<!-- Login -->
<form:form modelAttribute="userLogin">
<form:hidden path="idLogin"/>
<div class="form-group">
<label class="control-label col-lg-2" for="Email">E-mail</label>
<div class="col-lg-10">
<form:input id="Email" path="Email" type="text" class="form:input-large"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="Password">Password</label>
<div class="col-lg-10">
<form:password id="Password" path="Password" class="form:input-large"/>
</div>
</div>
</form:form>
<!-- Profile -->
<form:form modelAttribute="userProfiles">
<form:hidden path="id"/>
<div class="form-group">
<label class="control-label col-lg-2" for="FirstName">FirstName</label>
<div class="col-lg-10">
<form:input id="FirstName" path="FirstName" type="text" class="form:input-large"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="LastName">LastName</label>
<div class="col-lg-10">
<form:input id="LastName" path="LastName" type="text" class="form:input-large"/>
</div>
</div>
</form:form>
<!-- Users -->
<form:form modelAttribute="users">
<form:hidden path="idUser"/>
<div class="form-group">
<label class="control-label col-lg-2" for="idUserRole">Role</label>
<div class="col-lg-10">
<form:select path="userRole" items="${userRoless}"
itemValue="IdUserRole" itemLabel="name" multiple="false"
class="span12">
</form:select>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="IdLogin">Id</label>
<div class="col-lg-10">
<form:input id="userLogin" path="userLogin" type="text" class="form:input-large"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="IdLogin">Id</label>
<div class="col-lg-10">
<form:input id="userProfile" path="userProfile" type="text" class="form:input-large"/>
</div>
</div>
</div>
</form:form>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input type="submit" id="btnAdd" class="btn btn-primary" value ="Add"/>
</div>
</div>
</fieldset>
</form:form>
答案 0 :(得分:2)
注意:如果您将代表每个表的java类添加到您的问题中,那将非常有用。
您应该添加描述表格与您的类相关的元数据(pojos),并将CascadeType
设置为Persist
或ALL
,例如:
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "user_roles", referencedColumnName = "id")
private UserRoles userRole;
@OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER, orphanRemoval =
true)
@JoinColumn(name = "id_user_login", referencedColumnName = "id")
private UserLogin userLogin;
@OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER, orphanRemoval =
true)
@JoinColumn(name = "id_user_profile", referencedColumnName = "id")
private UserProfile userProfile;
然后,您可以设置子类/表的相应值并插入父类/表:
Users user = new Users();
UserProfile myUserProfile = new UserProfile();
UserLogin myUserLogin = new UserLogin();
UserRole myUserRole = new UserRole();
//... set the values ...
//then:
user.setUserProfile(myUserProfile);
user.setUserLogin(myUserLogin);
user.setUserRole(myUserRole);
[编辑]
我不确定你是如何坚持你的物体的,但我认为你是这样做的
服务(userProfilesService
等)。
您可以通过以下方法之一放置我上面写的内容: 1。
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAddNewUserForm(Model model) {
UserProfiles userProfiles = new UserProfiles();
UserLogins userLogins = new UserLogins();
Users users = new Users();
//Here
users.setUserProfile(userProfiles);
users.setUserLogins(userLogins);
userLogins.setUsers(users);
userProfiles.setUsers(users);
model.addAttribute("userProfiles", userProfiles);
model.addAttribute("users", users);
model.addAttribute("userLogin", userLogins);
model.addAttribute("userRoless", d_UserRolesService.getAllRoles());
return "userNew";
}
2。或
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String processAddNewUserForm(@ModelAttribute("userProfiles") UserProfiles profileToBeAdded,
@ModelAttribute("userLogin") UserLogins loginToBeAdded, @ModelAttribute("users") Users userToBeAdded,
HttpServletRequest request) {
//Or here
userToBeAdded.setUserProfile(profileToBeAdded);
userToBeAdded.setUserLogins(loginToBeAdded);
loginToBeAdded.setUsers(userToBeAdded);
profileToBeAdded.setUsers(userToBeAdded);
userProfilesService.addUser(profileToBeAdded);
loginsService.AddOrUpdateUser(loginToBeAdded);
usersService.AddOrUpdateUser(userToBeAdded);
return "redirect:/users";
}
[编辑2]
正如@Amogh指出的那样,你的观点也存在问题。据我所知,即使在JSP / JSF中你也应该有类似于下面的内容:
请注意:我不知道Spring并且在很长一段时间内没有使用JSP / JSF,所以这可能不是完全准确或最有效的方法。
<form:form class="form-horizontal" modelAttribute="users">
<fieldset>
<legend>New user</legend>
<!-- Login -->
<form:hidden path="idLogin"/>
<div class="form-group">
<label class="control-label col-lg-2" for="Email">E-mail</label>
<div class="col-lg-10">
<form:input id="Email" path="users.userLogin.email" type="text" class="form:input-large"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="Password">Password</label>
<div class="col-lg-10">
<form:password id="Password" path="users.userLogin.password" class="form:input-large"/>
</div>
</div>
</fieldset>
<fieldset>
<!-- Profile -->
<form:hidden path="id"/>
<div class="form-group">
<label class="control-label col-lg-2" for="FirstName">FirstName</label>
<div class="col-lg-10">
<form:input id="FirstName" path="users.userProfile.firstName" type="text" class="form:input-large"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="LastName">LastName</label>
<div class="col-lg-10">
<form:input id="LastName" path="users.userProfile.lastName" type="text" class="form:input-large"/>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input type="submit" id="btnAdd" class="btn btn-primary" value ="Add"/>
</div>
</div>
</fieldset>
</form:form>