최대 절전 모드로 변경되었습니다. 다음 코드는 일대 다 관계를 사용하여 부모 및 자식 테이블을 업데이트하는 데 사용됩니다. 하지만 개체 부모 테이블을 업데이트하는 경우에만 업데이트 할 때. 그리고 자식 테이블은 삽입되고 업데이트되지 않는 데 사용됩니다. 새 레코드를 삽입하고 업데이트합니다. 심지어 bidirectiononal에 대한 주석을 구성했습니다. 내 객체는One To Many 업데이트 프로세스에서 하위 테이블이 업데이트되지 않습니다.
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="students")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="student_id")
private long studentId=0;
@Column(name="sname",length=15)
private String studentName=null;
@Column(name="grp",length=15)
private String grp=null;
@OneToOne(mappedBy="parent")
private Address address;
@OneToMany(fetch=FetchType.EAGER,targetEntity=Courses.class,cascade=CascadeType.ALL)
@JoinColumn(name="stud_id",referencedColumnName="student_id")
private Set<Courses> courses=null;
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getGrp() {
return grp;
}
public void setGrp(String grp) {
this.grp = grp;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Set<Courses> getCourses() {
return courses;
}
public void setCourses(Set<Courses> courses) {
this.courses = courses;
}
}
을 따라하고 자식 개체입니다
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.ManyToOne;
@Entity
@Table(name="courses")
public class Courses {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="enroll_no")
private long enrollNo=0L;
@Column(name="course_id")
private long courseId=0L;
@Column(name="course_name")
private String courseName=null;
public long getCourseId() {
return courseId;
}
public void setCourseId(long courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="stud_id",referencedColumnName="student_id")
Student student=null;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
을 다음 그리고 난 업데이트 부모와 자식 객체에 데이터를 설정하는 데 사용됩니다. 다음 코드는 부모 개체와 자식 개체를 모두 업데이트하는 방식입니다. 여기에 학생 ID를 사용하여 자식 테이블 행을 업데이트하는 데 사용됩니다. 코스 ID로 하위 테이블 행을 업데이트해야합니다. 내 실수를 바로 잡아라.
System.out.println("Enter the Student Id");
studentId=sc.nextLong();
System.out.println("Enter the Student name.");
studentName=sc.next();
System.out.println("Enter the Blood Group of Student.");
group=sc.next();
System.out.println("Enter the Number of courses.");
noOfCourses=sc.nextLong();
courses=new LinkedHashSet<Courses>();
Courses courses2=null;
for(long l=0;l<noOfCourses;l++){
courses2=new Courses();
System.out.println("Enter the Student Course Id.");
studentCourseId=sc.nextLong();
System.out.println("Enter the Student Course Name");
courseName=sc.next();
courses2.setCourseId(studentCourseId);
courses2.setCourseName(courseName);
courses.add(courses2);
}
public void updateStudentDetailsOtmDao(long studentId,String studentName, String group, Set<Courses> courses) throws Exception{
Session session=null;
try{
if(factory!=null && !factory.isClosed()){
session=factory.openSession();
session.beginTransaction();
Student student=(Student)session.get(Student.class, studentId);
student.setStudentName(studentName);
student.setGrp(group);
student.setCourses(courses);
session.update(student);
session.getTransaction().commit();
System.out.println("Successfully updated the Student");
}else{
System.out.println("Please check the Session Factory");
}
}catch(HibernateException exception){
session.getTransaction().rollback();
throw exception;
}finally{
if(session!=null){
session.close();
}
}
}
안녕하세요 ladyNev. 당신이 말했듯이 나는 변화를했습니다. 그러나 그것은 단지 삭제되고 있습니다. 세부 정보를 업데이트하지 않습니다. –
@MuthuVikki 업데이트 된 '코스'에는 유효한 'studentCourseId'가 있는지 확인하십시오. –