简体   繁体   English

Spring Hibernate一对一关系在子表上创建新记录,而不是更新

[英]Spring Hibernate One-To-One relationship creates new record on child table instead of update

I'm new to Spring and still learning Hibernate One-to-one relationship. 我是Spring的新手,仍然在学习Hibernate一对一关系。 I have this problem that whenever I update a particular record it creates a new record for the child table and the foreign key in my owner table is also updated. 我有一个问题,每当更新特定记录时,它都会为子表创建一个新记录,并且所有者表中的外键也会更新。 What I'm expecting is that the records for the two joined tables will just only update. 我期望的是两个联接表的记录只会更新。

Here are some codes: 以下是一些代码:

Employee.java 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 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 EmployeeDAOImpl.java

public class EmployeeDAOImpl implements EmployeeDAO {

@Override
public Employee updateEmployee(Employee employee) {
    sessionFactory.getCurrentSession().update(employee);
    return employee;
}

EmployeeController.java 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 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>

Scenario: 场景:

Originally, column empUserId in tbl_employee is 1 which is linked to the first record of tbl_employee_user_account. 最初,tbl_employee中的empUserId列为1,该列链接到tbl_employee_user_account的第一条记录。 After I updated the name (from name1 to new-name1), tbl_employee_user_account creates new record with id 2 and the empUserId in tbl_employee changed into 2. 更新名称(从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    |
+-----------+----------+-----------+-----------+----------+

Put yourself in the shoes of Hibernate. 让自己陷入休眠状态。 You're being asked to update en employee. 系统要求您更新员工。 This employee is identified by an ID, and has a new name, a new address, etc. and also a new user account, instead of the old one, that already existed and was identified by an ID. 该员工由一个ID标识,并具有一个新的名称,新的地址等,并且还有一个新的用户帐户,而不是原来的旧用户帐户,该帐户已经由ID标识。 The new one doesn't have any ID. 新的没有任何ID。 So it can't possibly be the same account as the old one. 因此,它不可能与旧帐户相同。 So Hibernate just does what it's being told to do: replace the account of the employee by a new one. 因此,Hibernate只是按照其指示去做:用一个新的雇员帐户代替该雇员的帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM