简体   繁体   中英

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

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>

Scenario:

Originally, column empUserId in tbl_employee is 1 which is linked to the first record of 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.

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. The new one doesn't have any 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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