私は休止状態になっています。次のコードでは、1対多の関係を使用して親テーブルと子テーブルを更新しています。しかし、私はオブジェクトの親テーブルのみを更新して更新しています。そして、子テーブルは挿入され、更新されないために使用されます。新しいレコードを挿入して更新します。でも、私はbidirectonalのアノテーションを設定しました。私のオブジェクトは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;
}
}
を次のと私は、更新のために、親と子オブジェクトにデータを設定するために使用されます。次のコードは、親オブジェクトと子オブジェクトの両方を更新する私のアプローチです。ここで私は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更新された 'Courses'に有効な' studentCourseId'があることを確認してください –