我是Spring的新手,还在学习Hibernate一对一的关系。我有这个问题,每当我更新一个特定的记录时,它会为子表创建一个新记录,并且我的所有者表中的外键也会更新。我期待的是两个连接表的记录只会更新。
以下是一些代码:
Employee.java
public class Employee implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int empId;
@Column
private String name;
@Column
private String email;
@Column
private String address;
@Column
private String telephone;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="empUserId")
private EmployeeUserAccount employeeUserAccount;
//getters and setters
EmployeeUserAccount.java
public class EmployeeUserAccount implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int empUserId;
@Column
private String userName;
@Column
private String password;
@Column
private String userLevel;
@OneToOne(mappedBy="employeeUserAccount")
private Employee employee;
//getters and setters
EmployeeDAOImpl.java
public class EmployeeDAOImpl implements EmployeeDAO {
@Override
public Employee updateEmployee(Employee employee) {
sessionFactory.getCurrentSession().update(employee);
return employee;
}
EmployeeController.java
public class EmployeeController {
@RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("command") Employee employee) {
if (employee.getEmpId() == 0) {
employeeService.addEmployee(employee);
} else {
employeeService.updateEmployee(employee);
}
return new ModelAndView("redirect:/");
}
EmployeeForm.jsp
<body>
<div align="center">
<h1>New/Edit Employee</h1>
<form:form action="saveEmployee" method="post" >
<table>
<form:hidden path="empId"/>
<tr>
<td>Name:</td>
<td><form:input path="name" value="${employee.name}"/></td>
</tr>
<tr>
<td>Email:</td>
<td><form:input path="email" value="${employee.email}"/></td>
</tr>
<tr>
<td>Address:</td>
<td><form:input path="address" value="${employee.address}"/></td>
</tr>
<tr>
<td>Telephone:</td>
<td><form:input path="telephone" value="${employee.telephone}"/></td>
</tr>
<tr>
<td>Username:</td>
<td><form:input path="employeeUserAccount.userName" value="${employee.employeeUserAccount.userName}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><form:input path="employeeUserAccount.password" value="${employee.employeeUserAccount.password}"/></td>
</tr>
<tr>
<td>Role:</td>
<td>
<form:select path="employeeUserAccount.userLevel">
<c:forEach items="${role}" var="r">
<c:choose>
<c:when test="${r==employee.employeeUserAccount.userLevel}">
<option value="${r}" selected="true">${r}</option>
</c:when>
<c:otherwise>
<option value="${r}">${r}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>
</div>
情境:
最初,tbl_employee中的列empUserId为1,它链接到tbl_employee_user_account的第一条记录。更新名称后(从name1到new-name1),tbl_employee_user_account创建id为2的新记录,tbl_employee中的empUserId变为2。
mysql> select * from tbl_employee;
+-------+----------+--------+-----------+-----------+----------+
| empId | address | email | name | telephone | empUserId |
+-------+----------+--------+-----------+-----------+-----------+
| 1 | address1 | email1 | new-name1 | tele1 | 2 |
+-------+----------+--------+-----------+-----------+-----------+
mysql> select * from tbl_employee_user_account;
+-----------+----------+-----------+-----------+----------+
| empUserId | employee | password | userLevel | userName |
+-----------+----------+-----------+-----------+----------+
| 1 | NULL | password1 | Admin | user1 |
| 2 | NULL | password1 | Admin | user1 |
+-----------+----------+-----------+-----------+----------+
答案 0 :(得分:-1)
让自己置身于Hibernate之中。您被要求更新员工。此员工由ID标识,并且具有新名称,新地址等,还有新用户帐户,而不是旧用户帐户,已存在且由ID标识。新的没有任何ID。因此它不可能与旧帐户相同。因此,Hibernate只会做它被告知要做的事情:用新的帐户替换员工的帐户。