| 
					
						 Hi all,
  We've recently switched over to Hibernate (latest) and MySQL 5.1. We have an unidirectional ManyToMany relationship between two objects Student and Module. A Student has a Set of Modules, but the modules have no knowledge of the Students. There are many Students and many Modules.
  Our classes are set up as follows;
  @Entity public class Student extends User implements java.io.Serializable {
      @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST},fetch = FetchType.EAGER)     @JoinTable(             name="Student_EcafModule",             joinColumns = @JoinColumn( name="studentId"),             inverseJoinColumns = @JoinColumn( name="moduleId")     )     private Set<Module> modules;     etc etc...
  @Entity public class Module extends LightEntity implements java.io.Serializable {
      @Id     @GeneratedValue(strategy = GenerationType.AUTO)     private Long id;     etc etc...
  The code used to add a module to a student is as follows and is within an EJB;
  public void addModule(Student student, Module module) {         student = em.merge(student);         module = em.merge(module);         student.addModule(module);             }
  For example when two students take the same module the join table holds;
  studentId | moduleId   ______________________	       46	  |    1    47     |    1
  If the first student tries to take another module, then 46 , 2  should be added to the table, however we get this error instead;
  417906 [httpSSLWorkerThread-8080-1] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000 417906 [httpSSLWorkerThread-8080-1] ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry '46' for key 'students_id' 417906 [httpSSLWorkerThread-8080-1] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update etc etc...
  For some reason it is not letting us add another student --> module relationship for the same student. I cannot understand why this is so when it is a ManyToMany relationship. Is there something wrong with the way I add the module to the student? The table creation from MySQL is as follows;
  DROP TABLE IF EXISTS `student_ecafmodule`; CREATE TABLE `student_ecafmodule` (   `studentId` bigint(20) NOT NULL,   `moduleId` bigint(20) NOT NULL,   PRIMARY KEY (`studentId`,`moduleId`),   KEY `FKF7D006538203EAFF` (`moduleId`),   KEY `FKF7D00653EC387445` (`studentId`),   CONSTRAINT `FKF7D00653EC387445` FOREIGN KEY (`studentId`) REFERENCES `student` (`id`),   CONSTRAINT `FKF7D006538203EAFF` FOREIGN KEY (`moduleId`) REFERENCES `ecafmodule` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  So the primary key is a composite of studentId and moduleId and therefore the insert should work, but it doesn't. 
  Does anyone have any thoughts?
  Many thanks
  Stuart 
					
  
						
					 |