title: Hibernate 4, cannot persist an entity with composite primary key
body: <p>I want to persist an entity that contains a composite primary key. 
  the connection to the database and create the table it works but inserting a new line failed.</p>
  
  <p>Student.java:</p>
  
  <pre><code>@Entity
  @Table(name = "student")
  public class Student extends BaseEntity implements Serializable{
  
      /**
       * 
       */
      private static final long serialVersionUID = -6801103333234228955L;
  
      private Integer studentId;
      private String cin;
      private String firstName;
      private String lastName;
      private Integer age;
      private String phone;
      private String mail;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "STUDENT_ID", nullable = false)
      public Integer getStudentId() {
          return studentId;
      }
  
      public void setStudentId(Integer studentId) {
          this.studentId = studentId;
      }
  
      @Id
      @Column(name = "CIN", unique= true, nullable = false, length = 8)
      public String getCin() {
          return cin;
      }
  
      public void setCin(String cin) {
          this.cin = cin;
      }
  
      @Column(name = "FIRST_NAME", nullable = false, length = 25)
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      @Column(name = "LAST_NAME", nullable = false, length = 20)
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      @Column(name = "AGE")
      public Integer getAge() {
          return age;
      }
  
      public void setAge(Integer age) {
          this.age = age;
      }
  
      @Column(name = "PHONE", nullable = false, length = 20)
      public String getPhone() {
          return phone;
      }
  
      public void setPhone(String phone) {
          this.phone = phone;
      }
  
      @Column(name = "MAIL", length = 30)
      public String getMail() {
          return mail;
      }
  
      public void setMail(String mail) {
          this.mail = mail;
      }
  
  }
  </code></pre>
  
  <p>BaseEntity.java:</p>
  
  <pre><code>public abstract class BaseEntity {
  
      private String id = IdGenerator.createId();
  
      private Integer version = null;
  
      public String getId() {
          return this.id;
      }
  
      public void setId(String value) {
          this.id = value;
      }
  
      public Integer getVersion() {
          return this.version;
      }
  
      public void setVersion(Integer value) {
          this.version = value;
      }
  
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || !(o instanceof BaseEntity )) {
              return false;
          }
  
          BaseEntity other  = (BaseEntity ) o;
  
          // if the id is missing, return false
          if (id == null) return false;
  
          // equivalence by id
          return id.equals(other.getId());
      }
  
      public int hashCode() {
          if (id != null) {
              return id.hashCode();
          } else {
              return super.hashCode();
          }
      }
  
      public String toString() {
          return this.getClass().getName()
              + "[id=" + id + "]";
      }
  
      public boolean isCreation() {
          return version == null;
      }
  
      public void regenerateId() {
          id = IdGenerator.createId();
          version = null;
      }
  }
  </code></pre>
  
  <p>IdGenerator.java:</p>
  
  <pre><code>public class IdGenerator {
  
      public static String createId() {
          UUID uuid = java.util.UUID.randomUUID();
          return uuid.toString();
      }
  
  }
  </code></pre>
  
  <p>MainTest.java:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"/applicationContext.xml"})
  public class MainTest{
  
      private static final  Logger logger = LoggerFactory.getLogger(MainTest.class);
  
      @Autowired
      StudentDao studentDao;
  
      @Test
      public void testStudent(){
          Student student = new Student();
          student.setCin("05454447");
          student.setFirstName("student1");
          student.setLastName("student1");
          student.setAge(27);
          student.setPhone("4578979");
          student.setMail("d@d.com");
          Integer id = studentDao.create(student);
          logger.info("student id : "+ id);
      }
  
  }
  </code></pre>
  
  <p>The Log:</p>
  
  <pre><code>SLF4J: Class path contains multiple SLF4J bindings.
  SLF4J: Found binding in [jar:file:/C:/Users/SSAIDI/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  SLF4J: Found binding in [jar:file:/C:/Users/SSAIDI/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
  SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
  log4j:WARN No such property [minIndex] in org.apache.log4j.RollingFileAppender.
  log4j:WARN No such property [maxIndex] in org.apache.log4j.RollingFileAppender.
  12:01:14,187  INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from class path resource [applicationContext.xml]
  12:01:14,423  INFO GenericApplicationContext:513 - Refreshing org.springframework.context.support.GenericApplicationContext@20a6a283: startup date [Fri Aug 15 12:01:14 GMT+01:00 2014]; root of context hierarchy
  12:01:14,524  INFO PropertySourcesPlaceholderConfigurer:172 - Loading properties file from class path resource [persistence-mysql.properties]
  12:01:14,755 DEBUG BasicTypeRegistry:146 - Adding type registration boolean -&gt; org.hibernate.type.BooleanType@7d7d7924
  12:01:14,756 DEBUG BasicTypeRegistry:146 - Adding type registration boolean -&gt; org.hibernate.type.BooleanType@7d7d7924
  12:01:14,756 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Boolean -&gt; org.hibernate.type.BooleanType@7d7d7924
  12:01:14,757 DEBUG BasicTypeRegistry:146 - Adding type registration numeric_boolean -&gt; org.hibernate.type.NumericBooleanType@457def5b
  12:01:14,758 DEBUG BasicTypeRegistry:146 - Adding type registration true_false -&gt; org.hibernate.type.TrueFalseType@353ff34d
  12:01:14,759 DEBUG BasicTypeRegistry:146 - Adding type registration yes_no -&gt; org.hibernate.type.YesNoType@3a9c7d64
  12:01:14,761 DEBUG BasicTypeRegistry:146 - Adding type registration byte -&gt; org.hibernate.type.ByteType@969f4cd
  12:01:14,761 DEBUG BasicTypeRegistry:146 - Adding type registration byte -&gt; org.hibernate.type.ByteType@969f4cd
  12:01:14,761 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Byte -&gt; org.hibernate.type.ByteType@969f4cd
  12:01:14,762 DEBUG BasicTypeRegistry:146 - Adding type registration character -&gt; org.hibernate.type.CharacterType@530ee71b
  12:01:14,763 DEBUG BasicTypeRegistry:146 - Adding type registration char -&gt; org.hibernate.type.CharacterType@530ee71b
  12:01:14,763 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Character -&gt; org.hibernate.type.CharacterType@530ee71b
  12:01:14,765 DEBUG BasicTypeRegistry:146 - Adding type registration short -&gt; org.hibernate.type.ShortType@268aadef
  12:01:14,766 DEBUG BasicTypeRegistry:146 - Adding type registration short -&gt; org.hibernate.type.ShortType@268aadef
  12:01:14,767 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Short -&gt; org.hibernate.type.ShortType@268aadef
  12:01:14,768 DEBUG BasicTypeRegistry:146 - Adding type registration integer -&gt; org.hibernate.type.IntegerType@1f12853f
  12:01:14,768 DEBUG BasicTypeRegistry:146 - Adding type registration int -&gt; org.hibernate.type.IntegerType@1f12853f
  12:01:14,768 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Integer -&gt; org.hibernate.type.IntegerType@1f12853f
  12:01:14,770 DEBUG BasicTypeRegistry:146 - Adding type registration long -&gt; org.hibernate.type.LongType@191a248
  12:01:14,770 DEBUG BasicTypeRegistry:146 - Adding type registration long -&gt; org.hibernate.type.LongType@191a248
  12:01:14,770 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Long -&gt; org.hibernate.type.LongType@191a248
  12:01:14,772 DEBUG BasicTypeRegistry:146 - Adding type registration float -&gt; org.hibernate.type.FloatType@4443baa7
  12:01:14,772 DEBUG BasicTypeRegistry:146 - Adding type registration float -&gt; org.hibernate.type.FloatType@4443baa7
  12:01:14,772 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Float -&gt; org.hibernate.type.FloatType@4443baa7
  12:01:14,773 DEBUG BasicTypeRegistry:146 - Adding type registration double -&gt; org.hibernate.type.DoubleType@7909298b
  12:01:14,774 DEBUG BasicTypeRegistry:146 - Adding type registration double -&gt; org.hibernate.type.DoubleType@7909298b
  12:01:14,774 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Double -&gt; org.hibernate.type.DoubleType@7909298b
  12:01:14,776 DEBUG BasicTypeRegistry:146 - Adding type registration big_decimal -&gt; org.hibernate.type.BigDecimalType@4efdd8b
  12:01:14,776 DEBUG BasicTypeRegistry:146 - Adding type registration java.math.BigDecimal -&gt; org.hibernate.type.BigDecimalType@4efdd8b
  12:01:14,778 DEBUG BasicTypeRegistry:146 - Adding type registration big_integer -&gt; org.hibernate.type.BigIntegerType@513925a
  12:01:14,778 DEBUG BasicTypeRegistry:146 - Adding type registration java.math.BigInteger -&gt; org.hibernate.type.BigIntegerType@513925a
  12:01:14,779 DEBUG BasicTypeRegistry:146 - Adding type registration string -&gt; org.hibernate.type.StringType@79a70972
  12:01:14,779 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.String -&gt; org.hibernate.type.StringType@79a70972
  12:01:14,780 DEBUG BasicTypeRegistry:146 - Adding type registration nstring -&gt; org.hibernate.type.StringNVarcharType@4d83f728
  12:01:14,781 DEBUG BasicTypeRegistry:146 - Adding type registration ncharacter -&gt; org.hibernate.type.CharacterNCharType@16a1fa7f
  12:01:14,782 DEBUG BasicTypeRegistry:146 - Adding type registration url -&gt; org.hibernate.type.UrlType@368c7bd5
  12:01:14,782 DEBUG BasicTypeRegistry:146 - Adding type registration java.net.URL -&gt; org.hibernate.type.UrlType@368c7bd5
  12:01:14,784 DEBUG BasicTypeRegistry:146 - Adding type registration date -&gt; org.hibernate.type.DateType@55a71fa6
  12:01:14,784 DEBUG BasicTypeRegistry:146 - Adding type registration java.sql.Date -&gt; org.hibernate.type.DateType@55a71fa6
  12:01:14,786 DEBUG BasicTypeRegistry:146 - Adding type registration time -&gt; org.hibernate.type.TimeType@687f705e
  12:01:14,786 DEBUG BasicTypeRegistry:146 - Adding type registration java.sql.Time -&gt; org.hibernate.type.TimeType@687f705e
  12:01:14,788 DEBUG BasicTypeRegistry:146 - Adding type registration timestamp -&gt; org.hibernate.type.TimestampType@4caa1b37
  12:01:14,788 DEBUG BasicTypeRegistry:146 - Adding type registration java.sql.Timestamp -&gt; org.hibernate.type.TimestampType@4caa1b37
  12:01:14,788 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.Date -&gt; org.hibernate.type.TimestampType@4caa1b37
  12:01:14,789 DEBUG BasicTypeRegistry:146 - Adding type registration dbtimestamp -&gt; org.hibernate.type.DbTimestampType@7313c51
  12:01:14,791 DEBUG BasicTypeRegistry:146 - Adding type registration calendar -&gt; org.hibernate.type.CalendarType@65c98bf3
  12:01:14,791 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.Calendar -&gt; org.hibernate.type.CalendarType@65c98bf3
  12:01:14,791 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.GregorianCalendar -&gt; org.hibernate.type.CalendarType@65c98bf3
  12:01:14,792 DEBUG BasicTypeRegistry:146 - Adding type registration calendar_date -&gt; org.hibernate.type.CalendarDateType@551ea5f4
  12:01:14,793 DEBUG BasicTypeRegistry:146 - Adding type registration locale -&gt; org.hibernate.type.LocaleType@64bb9a0a
  12:01:14,793 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.Locale -&gt; org.hibernate.type.LocaleType@64bb9a0a
  12:01:14,794 DEBUG BasicTypeRegistry:146 - Adding type registration currency -&gt; org.hibernate.type.CurrencyType@75304935
  12:01:14,794 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.Currency -&gt; org.hibernate.type.CurrencyType@75304935
  12:01:14,795 DEBUG BasicTypeRegistry:146 - Adding type registration timezone -&gt; org.hibernate.type.TimeZoneType@1004243a
  12:01:14,796 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.TimeZone -&gt; org.hibernate.type.TimeZoneType@1004243a
  12:01:14,797 DEBUG BasicTypeRegistry:146 - Adding type registration class -&gt; org.hibernate.type.ClassType@ddc5a99
  12:01:14,797 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Class -&gt; org.hibernate.type.ClassType@ddc5a99
  12:01:14,798 DEBUG BasicTypeRegistry:146 - Adding type registration uuid-binary -&gt; org.hibernate.type.UUIDBinaryType@67728a26
  12:01:14,799 DEBUG BasicTypeRegistry:146 - Adding type registration java.util.UUID -&gt; org.hibernate.type.UUIDBinaryType@67728a26
  12:01:14,799 DEBUG BasicTypeRegistry:146 - Adding type registration uuid-char -&gt; org.hibernate.type.UUIDCharType@6ce18074
  12:01:14,800 DEBUG BasicTypeRegistry:146 - Adding type registration pg-uuid -&gt; org.hibernate.type.PostgresUUIDType@72f8516e
  12:01:14,801 DEBUG BasicTypeRegistry:146 - Adding type registration binary -&gt; org.hibernate.type.BinaryType@4a8716d3
  12:01:14,801 DEBUG BasicTypeRegistry:146 - Adding type registration byte[] -&gt; org.hibernate.type.BinaryType@4a8716d3
  12:01:14,802 DEBUG BasicTypeRegistry:146 - Adding type registration [B -&gt; org.hibernate.type.BinaryType@4a8716d3
  12:01:14,803 DEBUG BasicTypeRegistry:146 - Adding type registration wrapper-binary -&gt; org.hibernate.type.WrapperBinaryType@309c3c24
  12:01:14,803 DEBUG BasicTypeRegistry:146 - Adding type registration Byte[] -&gt; org.hibernate.type.WrapperBinaryType@309c3c24
  12:01:14,803 DEBUG BasicTypeRegistry:146 - Adding type registration [Ljava.lang.Byte; -&gt; org.hibernate.type.WrapperBinaryType@309c3c24
  12:01:14,804 DEBUG BasicTypeRegistry:146 - Adding type registration image -&gt; org.hibernate.type.ImageType@42357933
  12:01:14,805 DEBUG BasicTypeRegistry:146 - Adding type registration characters -&gt; org.hibernate.type.CharArrayType@4c71df84
  12:01:14,805 DEBUG BasicTypeRegistry:146 - Adding type registration char[] -&gt; org.hibernate.type.CharArrayType@4c71df84
  12:01:14,805 DEBUG BasicTypeRegistry:146 - Adding type registration [C -&gt; org.hibernate.type.CharArrayType@4c71df84
  12:01:14,806 DEBUG BasicTypeRegistry:146 - Adding type registration wrapper-characters -&gt; org.hibernate.type.CharacterArrayType@45196f3b
  12:01:14,807 DEBUG BasicTypeRegistry:146 - Adding type registration [Ljava.lang.Character; -&gt; org.hibernate.type.CharacterArrayType@45196f3b
  12:01:14,807 DEBUG BasicTypeRegistry:146 - Adding type registration Character[] -&gt; org.hibernate.type.CharacterArrayType@45196f3b
  12:01:14,808 DEBUG BasicTypeRegistry:146 - Adding type registration text -&gt; org.hibernate.type.TextType@1dc59111
  12:01:14,809 DEBUG BasicTypeRegistry:146 - Adding type registration ntext -&gt; org.hibernate.type.NTextType@7c68b91c
  12:01:14,813 DEBUG BasicTypeRegistry:146 - Adding type registration blob -&gt; org.hibernate.type.BlobType@3ff3a808
  12:01:14,813 DEBUG BasicTypeRegistry:146 - Adding type registration java.sql.Blob -&gt; org.hibernate.type.BlobType@3ff3a808
  12:01:14,814 DEBUG BasicTypeRegistry:146 - Adding type registration materialized_blob -&gt; org.hibernate.type.MaterializedBlobType@1273544a
  12:01:14,818 DEBUG BasicTypeRegistry:146 - Adding type registration clob -&gt; org.hibernate.type.ClobType@7389a769
  12:01:14,818 DEBUG BasicTypeRegistry:146 - Adding type registration java.sql.Clob -&gt; org.hibernate.type.ClobType@7389a769
  12:01:14,821 DEBUG BasicTypeRegistry:146 - Adding type registration nclob -&gt; org.hibernate.type.NClobType@1e78a60e
  12:01:14,822 DEBUG BasicTypeRegistry:146 - Adding type registration java.sql.NClob -&gt; org.hibernate.type.NClobType@1e78a60e
  12:01:14,822 DEBUG BasicTypeRegistry:146 - Adding type registration materialized_clob -&gt; org.hibernate.type.MaterializedClobType@48e24332
  12:01:14,823 DEBUG BasicTypeRegistry:146 - Adding type registration materialized_nclob -&gt; org.hibernate.type.MaterializedNClobType@c556032
  12:01:14,824 DEBUG BasicTypeRegistry:146 - Adding type registration serializable -&gt; org.hibernate.type.SerializableType@47a89031
  12:01:14,826 DEBUG BasicTypeRegistry:146 - Adding type registration object -&gt; org.hibernate.type.ObjectType@4ae50837
  12:01:14,827 DEBUG BasicTypeRegistry:146 - Adding type registration java.lang.Object -&gt; org.hibernate.type.ObjectType@4ae50837
  12:01:14,827 DEBUG BasicTypeRegistry:146 - Adding type registration imm_date -&gt; org.hibernate.type.AdaptedImmutableType@35413a25
  12:01:14,827 DEBUG BasicTypeRegistry:146 - Adding type registration imm_time -&gt; org.hibernate.type.AdaptedImmutableType@4d506e73
  12:01:14,828 DEBUG BasicTypeRegistry:146 - Adding type registration imm_timestamp -&gt; org.hibernate.type.AdaptedImmutableType@5b8367ac
  12:01:14,828 DEBUG BasicTypeRegistry:146 - Adding type registration imm_dbtimestamp -&gt; org.hibernate.type.AdaptedImmutableType@100b7c24
  12:01:14,828 DEBUG BasicTypeRegistry:146 - Adding type registration imm_calendar -&gt; org.hibernate.type.AdaptedImmutableType@620327b6
  12:01:14,828 DEBUG BasicTypeRegistry:146 - Adding type registration imm_calendar_date -&gt; org.hibernate.type.AdaptedImmutableType@3d244fff
  12:01:14,828 DEBUG BasicTypeRegistry:146 - Adding type registration imm_binary -&gt; org.hibernate.type.AdaptedImmutableType@1b000db5
  12:01:14,829 DEBUG BasicTypeRegistry:146 - Adding type registration imm_serializable -&gt; org.hibernate.type.AdaptedImmutableType@2083f3ec
  12:01:14,847  INFO Version:66 - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
  12:01:14,853  INFO Version:54 - HHH000412: Hibernate Core {4.3.6.Final}
  12:01:14,855  INFO Environment:239 - HHH000206: hibernate.properties not found
  12:01:14,857  INFO Environment:346 - HHH000021: Bytecode provider name : javassist
  12:01:15,199  INFO Dialect:145 - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
  12:01:15,343  INFO TransactionFactoryInitiator:62 - HHH000399: Using default transaction strategy (direct JDBC transactions)
  12:01:15,348  INFO ASTQueryTranslatorFactory:47 - HHH000397: Using ASTQueryTranslatorFactory
  12:01:15,369 TRACE TypeFactory:72 - Scoping types to session factory org.hibernate.internal.SessionFactoryImpl@250ea0db
  12:01:15,561  INFO SchemaExport:343 - HHH000227: Running hbm2ddl schema export
  Hibernate: drop table if exists student
  Hibernate: create table student (STUDENT_ID integer not null, CIN varchar(8) not null, AGE integer, FIRST_NAME varchar(25) not null, LAST_NAME varchar(20) not null, MAIL varchar(30), PHONE varchar(20) not null, primary key (STUDENT_ID, CIN))
  Hibernate: alter table student add constraint UK_lj69eor860a2au55t1v2srr90  unique (STUDENT_ID)
  Hibernate: alter table student add constraint UK_eilt0rp8h8wnge0723nacyv84  unique (CIN)
  12:01:16,313  INFO SchemaExport:405 - HHH000230: Schema export complete
  12:01:16,405  INFO HibernateTransactionManager:339 - Using DataSource [org.apache.commons.dbcp2.BasicDataSource@78cbe299] of Hibernate SessionFactory for HibernateTransactionManager
  12:01:16,514 ERROR BasicPropertyAccessor:121 - HHH000123: IllegalArgumentException in class: com.souhaieb.education.cursus.entities.Student, setter method of property: studentId
  12:01:16,514 ERROR BasicPropertyAccessor:122 - HHH000091: Expected type: java.lang.Integer, actual value: org.hibernate.id.IdentifierGeneratorHelper$2
  12:01:16,524  INFO GenericApplicationContext:873 - Closing org.springframework.context.support.GenericApplicationContext@20a6a283: startup date [Fri Aug 15 12:01:14 GMT+01:00 2014]; root of context hierarchy
  </code></pre>