Many to many relationship with composite primary key session.update error


#1

When I try to use session.update to update the StudentCourse obj,always give the error message:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of com.yp.entity.StudentCourse was altered from com.yp.entity.StudentCourseId@420 to com.yp.entity.StudentCourseId@41f

The session.save,session.delete,session.get work fine. How can I use session.update to update the composite primary key defined in Student_Course table, If the hql works, I don’t know how to write it to represent the primary key.
The code fragment of updating:

                StudentCourseId scId = new StudentCourseId();
		scId.setCourseId(3);
		scId.setStudentId(2);
		Transaction t = session.beginTransaction();
		StudentCourse sc = session.get(StudentCourse.class, scId);
		StudentCourseId scId2 = new StudentCourseId();
		scId2.setCourseId(sc.getPrimaryKey().getCourseId());
		scId2.setStudentId(1);
		sc.setPrimaryKey(scId2);
		session.update(sc);
		session.flush();//encounter error here.
		t.commit();

The sql:

create table Course(
	Course_ID int identity,
	Course_Name varchar(20) not null,
	constraint PK_Course primary key(Course_ID)
);

create table Student(
	Student_ID int identity,
	Student_Name varchar(20) not null,
	constraint PK_Student primary key(Student_ID)
);

create table Student_Course(
	Course_Student_CourseID int not null,
	Course_Student_StudentID int not null,
        Course_Student_Score int,
	constraint PK_Course_Student primary key(Course_Student_CourseID,Course_Student_StudentID),
	constraint FK_CourseID foreign key(Course_Student_CourseID) references Course(Course_ID),
	constraint FK_StudentID foreign key(Course_Student_StudentID) references Student(Student_ID),
);

The xml mapping file:

<class name="Student" table="Student">
		<id name="id" column="Student_ID">	
			<generator class="native"/>
		</id>		
		<property name="name" column="Student_Name" length="20"/>
		
		<set name="scSet" inverse="true" lazy="true">
			<key column="Course_Student_StudentID"/>
			<one-to-many class="StudentCourse"/>
		</set>
				
	</class>

<class name="Course" table="Course">
		<id name="id" column="Course_ID">	
			<generator class="native"/>
		</id>		
		<property name="name" column="Course_Name" length="20"/>
				
		<set name="scSet" inverse="true" lazy="true">
			<key column="Course_Student_CourseID"/>
			<one-to-many class="StudentCourse"/>
		</set>
				
	</class>


<class name="StudentCourse" table="Student_Course">

		<composite-id name="primaryKey" class="StudentCourseId">
			<key-property name="studentId" column="Course_Student_StudentID"/>
			<key-property name="courseId" column="Course_Student_CourseID"/>
		</composite-id>
		<property name="score" column="Course_Student_Score"/>
		
		<many-to-one name="student" class="Student" column="Course_Student_StudentID" insert="false" update="false"/>
		<many-to-one name="course" class="Course" column="Course_Student_CourseID" insert="false" update="false"/>

	</class>

#2

The Session update method is for reattaching a detached entity. It’s not for generating an UPDATE statement.

For more details, check out the JPA entity state transitions and how they relate to DML statements.