当更新Employee表时,为什么要在Address表中插入新行。我也想要简单的更新地址表

时间:2019-01-11 10:21:18

标签: spring hibernate

注意:当更新Employee时,为什么要在Address表中插入新行。我想更新Employee,也想更新地址表,而不是插入新行。

执行以下命令后:

session = sessionFactory.getCurrentSession();
session.update(employee);

结果:

休眠:在地址(地址,employee_empId,移动电话,密码)中插入值(?,?,?,?) 休眠状态:更新员工集addId = ?、 empName = ?、 empProfile =?哪里empId =?

@Entity
@Table(name = "Address")
public class Address {

    @Id
    @Column(name = "addId", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int addId;

    @Column(name = "pincode", unique = false, nullable = false, length = 100)
    int pincode;

    @Column(name = "address", unique = false, nullable = false, length = 100)
    String address;

    @Column(name = "mobile", unique = false, nullable = false, length = 100)
    String mobile;

    @OneToOne
    private Employee employee; 

    public int getAddId() {
        return addId;
    }

    public void setAddId(int addId) {
        this.addId = addId;
    }

    public int getPincode() {
        return pincode;
    }

    public void setPincode(int pincode) {
        this.pincode = pincode;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
}

@Entity
@Table(name = "Employee")
public class Employee {

    @Id
    @Column(name = "empId", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int empId;

    @Column(name = "empName", unique = false, nullable = false, length = 100)
    String empName;

    @Column(name = "empProfile", unique = false, nullable = false, length = 100)
    String empProfile;

    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name="addId")
    Address address;   

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getEmpProfile() {
        return empProfile;
    }

    public void setEmpProfile(String empProfile) {
        this.empProfile = empProfile;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

<form:form cssClass="form-horizontal" method="POST" action="${pageContext.request.contextPath}/editEmployee" modelAttribute="employee">
    <input type="hidden" value="${employeeDetail.empId}" name="empId">
    <div class="form-group">
      <label for="email">Employee Name:</label>
      <input type="text" class="form-control" name="empName" value="${employeeDetail.empName}">
    </div>

    <div class="form-group">
      <label for="pwd">Profile:</label> 
      <textarea rows="5" class="form-control" name="empProfile">${employeeDetail.empProfile}</textarea>
    </div>

    <div class="form-group">
      <label for="pwd">Pincode:</label> 
      <input type="text" class="form-control" name="address.pincode" value="${employeeDetail.address.pincode}">
    </div>

    <div class="form-group">
      <label for="pwd">Address:</label> 
      <textarea rows="5" class="form-control" name="address.address">${employeeDetail.address.address}</textarea>
    </div>

    <div class="form-group">
      <label for="pwd">Mobile No:</label>   
      <input type="text" class="form-control" name="address.mobile" value="${employeeDetail.address.mobile}">
    </div>

    <div class="form-group">
      <button type="submit" class="btn btn-primary">Update</button>
    </div>

  </form:form>


After execute following:

session=sessionFactory.getCurrentSession();         
session.update(employee);

Result:

Hibernate: insert into Address (address, employee_empId, mobile, pincode) values (?, ?, ?, ?)
Hibernate: update Employee set addId=?, empName=?, empProfile=? where empId=?

Note: When update Employee why new row inserted in Address table. I want to update Employee and want to update Address table also rather than new row inserted.

0 个答案:

没有答案