title: MySQLIntegrityConstraintViolationException
body: <p>i implemented one to many relationship in hibernate bt i m getting an error for duplicate key , so bit confused as to what to do
  if anyone can rectify my mistake it wld be a grt help , its been actually just quite a few days i m into hibernate </p>
  
  <p>this is my college class</p>
  
  <pre><code>package com.hibernate.arjun3;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  
  @Entity
  public class College {
  
  private String name;
  @Id
  @GeneratedValue
  private int college_id;
  private String location;
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public int getCollege_id() {
      return college_id;
  }
  
  public void setCollege_id(int college_id) {
      this.college_id = college_id;
  }
  
  public String getLocation() {
      return location;
  }
  
  public void setLocation(String location) {
      this.location = location;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my Student class</p>
  
  <pre><code>package com.hibernate.arjun3;
  
  import java.util.ArrayList;
  import java.util.Collection;
  
  import javax.persistence.CascadeType;
  import javax.persistence.ElementCollection;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinTable;
  import javax.persistence.OneToMany;
  import javax.persistence.JoinColumn;
  
  @Entity
  public class Students {
  
  private String firstname;
  private String lastname;
  @Id
  @GeneratedValue
  private int college_id;
  
  @ElementCollection
  @OneToMany(cascade = CascadeType.ALL)
  @JoinTable(name="College_Students" , joinColumns=@JoinColumn(name="College_Id"))
  private Collection&lt;College&gt; college = new ArrayList&lt;College&gt;();
  
  public Collection&lt;College&gt; getCollege() {
      return college;
  }
  
  public void setCollege(Collection&lt;College&gt; college) {
      this.college = college;
  }
  
  public String getFirstname() {
      return firstname;
  }
  
  public void setFirstname(String firstname) {
      this.firstname = firstname;
  }
  
  public String getLastname() {
      return lastname;
  }
  
  public void setLastname(String lastname) {
      this.lastname = lastname;
  }
  
  public int getCollege_id() {
      return college_id;
  }
  
  public void setCollege_id(int college_id) {
      this.college_id = college_id;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My Main class</p>
  
  <pre><code>package com.hibernate.arjun3;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.AnnotationConfiguration;
  
  public class Main1 {
  
  public static void main(String args[]) {
      College colg = new College();
      colg.setName("Vivekanand");
      colg.setLocation("Chembur");
  
      Students students = new Students();
      students.setFirstname("Arjun");
      students.setLastname("Narahari");
  
      Students students2 = new Students();
      students2.setFirstname("Sagar");
      students2.setLastname("Abhyankar");
  
      students.getCollege().add(colg);
      students2.getCollege().add(colg);
  
      SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
      Session session = factory.openSession();
      session.beginTransaction();
  
      session.save(students);
      session.save(students2);
      session.getTransaction().commit();
      session.close();
      factory.close();
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>The Error:</strong></p>
  
  <p>INFO: HHH000227: Running hbm2ddl schema export
  Hibernate: alter table Students_College drop foreign key FK_qk8e1fl454umkjvarmovmdgrp
  Hibernate: alter table Students_College drop foreign key FK_n2cycly15ecddgky71345r3u
  Hibernate: drop table if exists College
  Hibernate: drop table if exists Students
  Hibernate: drop table if exists Students_College
  Hibernate: create table College (college_id integer not null auto_increment, location varchar(255), name varchar(255), primary key (college_id))
  Hibernate: create table Students (college_id integer not null auto_increment, firstname varchar(255), lastname varchar(255), primary key (college_id))
  Hibernate: create table Students_College (Students_college_id integer not null, college_college_id integer not null)
  Hibernate: alter table Students_College add constraint UK_qk8e1fl454umkjvarmovmdgrp  unique (college_college_id)
  Hibernate: alter table Students_College add constraint FK_qk8e1fl454umkjvarmovmdgrp foreign key (college_college_id) references College (college_id)
  Hibernate: alter table Students_College add constraint FK_n2cycly15ecddgky71345r3u foreign key (Students_college_id) references Students (college_id)
  Sep 27, 2014 12:21:01 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
  INFO: HHH000230: Schema export complete
  Hibernate: insert into Students (firstname, lastname) values (?, ?)
  Hibernate: insert into College (location, name) values (?, ?)
  Hibernate: insert into Students (firstname, lastname) values (?, ?)
  Hibernate: insert into Students_College (Students_college_id, college_college_id) values (?, ?)
  Hibernate: insert into Students_College (Students_college_id, college_college_id) values (?, ?)
  Sep 27, 2014 12:21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 1062, SQLState: 23000
  Sep 27, 2014 12:21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: Duplicate entry '1' for key 'UK_qk8e1fl454umkjvarmovmdgrp'
  Sep 27, 2014 12:21:01 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
  INFO: HHH000010: On release of batch it still contained JDBC statements
  Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
      at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
      at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
      at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)
      at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
      at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
      at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
      at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
      at com.hibernate.arjun3.Main1.main(Main1.java:31)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'UK_qk8e1fl454umkjvarmovmdgrp'
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
      at com.mysql.jdbc.Util.getInstance(Util.java:383)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4208)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4140)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
      ... 12 more</p>
  
  <p>hoping for replies from u all experienced ppl
  thanking you</p>