Примечание. При обновлении Сотрудник почему новая строка вставляется в таблицу адресов. Я хочу обновить Employee и обновить адресную таблицу, а не вставлять новую строку.
После выполнения следующего:
сессия = sessionFactory.getCurrentSession ();
Session.update (работник);
Результат:
Hibernate: вставить в адрес (адрес, employee_empId, мобильный, пин-код) значения (?,?,?,?)
Hibernate: обновление набора сотрудников 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.