title: Unable to build Hibernate SessionFactory, Caused by: org.hibernate.AnnotationException:
body: <p>I have this issue with my mysql database and the classes generated by hibernate-tools, all was working well until I made a change in the db involving 4 tables.</p>
  
  <p>This are the tables:</p>
  
  <p>--</p>
  
  <pre><code>SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=2;
  SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
  SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
  
  
  
  -- -----------------------------------------------------
  -- Table `profile`
  -- -----------------------------------------------------
  
  CREATE TABLE IF NOT EXISTS `fairtime`.`profile` (
    `profile_id` INT(11) NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME NOT NULL,
    `updated_at` DATETIME NULL DEFAULT NULL,
    `app_user_id` INT(11) NOT NULL,
    `address` VARCHAR(45) NOT NULL,
    `phone` VARCHAR(45) NOT NULL,
    `city_id` INT(11) NOT NULL,
    PRIMARY KEY (`profile_id`),
    INDEX `fk_profile_app_user1_idx` (`app_user_id` ASC),
    INDEX `fk_profile_city1_idx` (`city_id` ASC),
    CONSTRAINT `fk_profile_app_user1`
      FOREIGN KEY (`app_user_id`)
      REFERENCES `fairtime`.`app_user` (`app_user_id`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fk_profile_city1`
      FOREIGN KEY (`city_id`)
      REFERENCES `fairtime`.`city` (`city_id`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB
  DEFAULT CHARACTER SET = utf8;
  
  
  
  -- -----------------------------------------------------
  -- Table `profile_option`
  -- -----------------------------------------------------
  
  CREATE TABLE IF NOT EXISTS `fairtime`.`profile_option` (
    `profile_option_id` INT(11) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `hidden_for_user` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'Specify if a target option is readable by Advertiser interface or not',
    `hidden_for_advertiser` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'Specify if a target option is readable by Advertiser interface or not',
    `hidden_for_offer` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'Specify if a target option is readable by Advertiser interface or not',
    `type` VARCHAR(45) NULL DEFAULT NULL,
    `is_unique_for_profile` TINYINT(1) NULL DEFAULT '0',
    `is_unique_for_target` TINYINT(1) NULL DEFAULT '0',
    PRIMARY KEY (`profile_option_id`))
  ENGINE = InnoDB
  DEFAULT CHARACTER SET = utf8;
  
  
  
  -- -----------------------------------------------------
  -- Table `profile_option_element`
  -- -----------------------------------------------------
  
  CREATE TABLE IF NOT EXISTS `fairtime`.`profile_option_element` (
    `profile_option_element_id` INT(11) NOT NULL AUTO_INCREMENT,
    `profile_option_id` INT(11) NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `hidden_for_user` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'Specify if a target option is readable by Advertiser interface or not',
    `hidden_for_advertiser` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'Specify if a target option is readable by Advertiser interface or not',
    `hidden_for_offer` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'Specify if a target option is readable by Advertiser interface or not',
    `type` VARCHAR(45) NULL DEFAULT NULL,
    `app_user_id` INT(11) NULL DEFAULT NULL COMMENT 'When a user creates a brand in their MyFairTime',
    `approved` TINYINT(1) NOT NULL DEFAULT '0',
    `created_at` DATETIME NULL DEFAULT NULL,
    `boolean_value` TINYINT(1) NULL DEFAULT NULL,
    PRIMARY KEY (`profile_option_element_id`, `profile_option_id`),
    INDEX `fk_target_option_element_target_option1_idx` (`profile_option_id` ASC),
    INDEX `fk_profile_option_element_app_user1_idx` (`app_user_id` ASC),
    CONSTRAINT `fk_target_option_element_target_option1`
      FOREIGN KEY (`profile_option_id`)
      REFERENCES `fairtime`.`profile_option` (`profile_option_id`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fk_profile_option_element_app_user1`
      FOREIGN KEY (`app_user_id`)
      REFERENCES `fairtime`.`app_user` (`app_user_id`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB
  DEFAULT CHARACTER SET = utf8;
  
  
  -- -----------------------------------------------------
  -- Table `profile_has_profile_option_element`
  -- -----------------------------------------------------
  
  CREATE TABLE IF NOT EXISTS `fairtime`.`profile_has_profile_option_element` (
    `profile_profile_id` INT(11) NOT NULL,
    `profile_option_element_profile_option_element_id` INT(11) NOT NULL,
    `profile_option_element_profile_option_id` INT(11) NOT NULL,
    PRIMARY KEY (`profile_profile_id`, `profile_option_element_profile_option_element_id`, `profile_option_element_profile_option_id`),
    INDEX `fk_profile_has_profile_option_element_profile_option_elemen_idx` (`profile_option_element_profile_option_element_id` ASC, `profile_option_element_profile_option_id` ASC),
    INDEX `fk_profile_has_profile_option_element_profile1_idx` (`profile_profile_id` ASC),
    CONSTRAINT `fk_profile_has_profile_option_element_profile1`
      FOREIGN KEY (`profile_profile_id`)
      REFERENCES `fairtime`.`profile` (`profile_id`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
    CONSTRAINT `fk_profile_has_profile_option_element_profile_option_element1`
      FOREIGN KEY (`profile_option_element_profile_option_element_id` , `profile_option_element_profile_option_id`)
      REFERENCES `fairtime`.`profile_option_element` (`profile_option_element_id` , `profile_option_id`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  ENGINE = InnoDB
  DEFAULT CHARACTER SET = utf8;
  
  
  SET SQL_MODE=@OLD_SQL_MODE;
  SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
  SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
  </code></pre>
  
  <p>--</p>
  
  <p>The schema with workbench is this:</p>
  
  <p><img src="https://i.stack.imgur.com/QUPAQ.png" alt="enter image description here"></p>
  
  <p>This are the classes involved in hibernate:</p>
  
  <p>Profile.java: </p>
  
  <p>--</p>
  
  <pre><code>package models.classes_hibernate;
  // Generated 02/07/2014 10:54:27 by Hibernate Tools 3.6.0
  
  
  import javax.persistence.*;
  import java.util.Date;
  import java.util.HashSet;
  import java.util.Set;
  
  import static javax.persistence.GenerationType.IDENTITY;
  
  /**
   * Profile generated by hbm2java
   */
  @Entity
  @Table(name="profile"
      ,catalog="fairtime"
  )
  public class Profile  implements java.io.Serializable {
  
  
       private Integer profileId;
       private AppUser appUser;
       private City city;
       private Date createdAt;
       private Date updatedAt;
       private String address;
       private String phone;
       private Set&lt;ProfileHasCampaign&gt; profileHasCampaigns = new HashSet&lt;ProfileHasCampaign&gt;(0);
       private Set&lt;ProfileOptionElement&gt; profileOptionElements = new HashSet&lt;ProfileOptionElement&gt;(0);
       private Set&lt;Offer&gt; offers = new HashSet&lt;Offer&gt;(0);
  
      public Profile() {
      }
  
  
      public Profile(AppUser appUser, City city, Date createdAt, String address, String phone) {
          this.appUser = appUser;
          this.city = city;
          this.createdAt = createdAt;
          this.address = address;
          this.phone = phone;
      }
      public Profile(AppUser appUser, City city, Date createdAt, Date updatedAt, String address, String phone, Set&lt;ProfileHasCampaign&gt; profileHasCampaigns, Set&lt;ProfileOptionElement&gt; profileOptionElements, Set&lt;Offer&gt; offers) {
         this.appUser = appUser;
         this.city = city;
         this.createdAt = createdAt;
         this.updatedAt = updatedAt;
         this.address = address;
         this.phone = phone;
         this.profileHasCampaigns = profileHasCampaigns;
         this.profileOptionElements = profileOptionElements;
         this.offers = offers;
      }
  
       @Id @GeneratedValue(strategy=IDENTITY)
  
  
      @Column(name="profile_id", unique=true, nullable=false)
      public Integer getProfileId() {
          return this.profileId;
      }
  
      public void setProfileId(Integer profileId) {
          this.profileId = profileId;
      }
  
  @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="app_user_id", nullable=false)
      public AppUser getAppUser() {
          return this.appUser;
      }
  
      public void setAppUser(AppUser appUser) {
          this.appUser = appUser;
      }
  
  @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="city_id", nullable=false)
      public City getCity() {
          return this.city;
      }
  
      public void setCity(City city) {
          this.city = city;
      }
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name="created_at", nullable=false, length=19)
      public Date getCreatedAt() {
          return this.createdAt;
      }
  
      public void setCreatedAt(Date createdAt) {
          this.createdAt = createdAt;
      }
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name="updated_at", length=19)
      public Date getUpdatedAt() {
          return this.updatedAt;
      }
  
      public void setUpdatedAt(Date updatedAt) {
          this.updatedAt = updatedAt;
      }
  
  
      @Column(name="address", nullable=false, length=45)
      public String getAddress() {
          return this.address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
  
      @Column(name="phone", nullable=false, length=45)
      public String getPhone() {
          return this.phone;
      }
  
      public void setPhone(String phone) {
          this.phone = phone;
      }
  
  @OneToMany(fetch=FetchType.LAZY, mappedBy="profile")
      public Set&lt;ProfileHasCampaign&gt; getProfileHasCampaigns() {
          return this.profileHasCampaigns;
      }
  
      public void setProfileHasCampaigns(Set&lt;ProfileHasCampaign&gt; profileHasCampaigns) {
          this.profileHasCampaigns = profileHasCampaigns;
      }
  
  @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="profile_has_profile_option_element", catalog="fairtime", joinColumns = { 
          @JoinColumn(name="profile_profile_id", nullable=false, updatable=false) }, inverseJoinColumns = { 
          @JoinColumn(name="profile_option_element_profile_option_element_id", nullable=false, updatable=false) })
      public Set&lt;ProfileOptionElement&gt; getProfileOptionElements() {
          return this.profileOptionElements;
      }
  
      public void setProfileOptionElements(Set&lt;ProfileOptionElement&gt; profileOptionElements) {
          this.profileOptionElements = profileOptionElements;
      }
  
  @OneToMany(fetch=FetchType.LAZY, mappedBy="profile")
      public Set&lt;Offer&gt; getOffers() {
          return this.offers;
      }
  
      public void setOffers(Set&lt;Offer&gt; offers) {
          this.offers = offers;
      }
  
  
  
  
  }
  </code></pre>
  
  <p>--</p>
  
  <p>ProfileOptionElement.java: </p>
  
  <p>--</p>
  
  <pre><code>package models.classes_hibernate;
  // Generated 02/07/2014 10:54:27 by Hibernate Tools 3.6.0
  
  
  import javax.persistence.*;
  import java.util.Date;
  import java.util.HashSet;
  import java.util.Set;
  
  /**
   * ProfileOptionElement generated by hbm2java
   */
  @Entity
  @Table(name="profile_option_element"
      ,catalog="fairtime"
  )
  public class ProfileOptionElement  implements java.io.Serializable {
  
  
       private ProfileOptionElementId id;
       private AppUser appUser;
       private ProfileOption profileOption;
       private String name;
       private boolean hiddenForUser;
       private boolean hiddenForAdvertiser;
       private boolean hiddenForOffer;
       private String type;
       private boolean approved;
       private Date createdAt;
       private Boolean booleanValue;
       private Set&lt;Profile&gt; profiles = new HashSet&lt;Profile&gt;(0);
       private Set&lt;TargetHasProfileOptionElement&gt; targetHasProfileOptionElements = new HashSet&lt;TargetHasProfileOptionElement&gt;(0);
  
      public ProfileOptionElement() {
      }
  
  
      public ProfileOptionElement(ProfileOptionElementId id, ProfileOption profileOption, String name, boolean hiddenForUser, boolean hiddenForAdvertiser, boolean hiddenForOffer, boolean approved) {
          this.id = id;
          this.profileOption = profileOption;
          this.name = name;
          this.hiddenForUser = hiddenForUser;
          this.hiddenForAdvertiser = hiddenForAdvertiser;
          this.hiddenForOffer = hiddenForOffer;
          this.approved = approved;
      }
      public ProfileOptionElement(ProfileOptionElementId id, AppUser appUser, ProfileOption profileOption, String name, boolean hiddenForUser, boolean hiddenForAdvertiser, boolean hiddenForOffer, String type, boolean approved, Date createdAt, Boolean booleanValue, Set&lt;Profile&gt; profiles, Set&lt;TargetHasProfileOptionElement&gt; targetHasProfileOptionElements) {
         this.id = id;
         this.appUser = appUser;
         this.profileOption = profileOption;
         this.name = name;
         this.hiddenForUser = hiddenForUser;
         this.hiddenForAdvertiser = hiddenForAdvertiser;
         this.hiddenForOffer = hiddenForOffer;
         this.type = type;
         this.approved = approved;
         this.createdAt = createdAt;
         this.booleanValue = booleanValue;
         this.profiles = profiles;
         this.targetHasProfileOptionElements = targetHasProfileOptionElements;
      }
  
       @EmbeddedId
  
  
      @AttributeOverrides( {
          @AttributeOverride(name="profileOptionElementId", column=@Column(name="profile_option_element_id", nullable=false) ), 
          @AttributeOverride(name="profileOptionId", column=@Column(name="profile_option_id", nullable=false) ) } )
      public ProfileOptionElementId getId() {
          return this.id;
      }
  
      public void setId(ProfileOptionElementId id) {
          this.id = id;
      }
  
  @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="app_user_id")
      public AppUser getAppUser() {
          return this.appUser;
      }
  
      public void setAppUser(AppUser appUser) {
          this.appUser = appUser;
      }
  
  @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="profile_option_id", nullable=false, insertable=false, updatable=false)
      public ProfileOption getProfileOption() {
          return this.profileOption;
      }
  
      public void setProfileOption(ProfileOption profileOption) {
          this.profileOption = profileOption;
      }
  
  
      @Column(name="name", nullable=false)
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      @Column(name="hidden_for_user", nullable=false)
      public boolean isHiddenForUser() {
          return this.hiddenForUser;
      }
  
      public void setHiddenForUser(boolean hiddenForUser) {
          this.hiddenForUser = hiddenForUser;
      }
  
  
      @Column(name="hidden_for_advertiser", nullable=false)
      public boolean isHiddenForAdvertiser() {
          return this.hiddenForAdvertiser;
      }
  
      public void setHiddenForAdvertiser(boolean hiddenForAdvertiser) {
          this.hiddenForAdvertiser = hiddenForAdvertiser;
      }
  
  
      @Column(name="hidden_for_offer", nullable=false)
      public boolean isHiddenForOffer() {
          return this.hiddenForOffer;
      }
  
      public void setHiddenForOffer(boolean hiddenForOffer) {
          this.hiddenForOffer = hiddenForOffer;
      }
  
  
      @Column(name="type", length=45)
      public String getType() {
          return this.type;
      }
  
      public void setType(String type) {
          this.type = type;
      }
  
  
      @Column(name="approved", nullable=false)
      public boolean isApproved() {
          return this.approved;
      }
  
      public void setApproved(boolean approved) {
          this.approved = approved;
      }
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name="created_at", length=19)
      public Date getCreatedAt() {
          return this.createdAt;
      }
  
      public void setCreatedAt(Date createdAt) {
          this.createdAt = createdAt;
      }
  
  
      @Column(name="boolean_value")
      public Boolean getBooleanValue() {
          return this.booleanValue;
      }
  
      public void setBooleanValue(Boolean booleanValue) {
          this.booleanValue = booleanValue;
      }
  
  @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="profile_has_profile_option_element", catalog="fairtime", joinColumns = { 
          @JoinColumn(name="profile_option_element_profile_option_element_id", nullable=false, updatable=false), 
          @JoinColumn(name="profile_option_element_profile_option_id", nullable=false, updatable=false) }, inverseJoinColumns = { 
          @JoinColumn(name="profile_profile_id", nullable=false, updatable=false) })
      public Set&lt;Profile&gt; getProfiles() {
          return this.profiles;
      }
  
      public void setProfiles(Set&lt;Profile&gt; profiles) {
          this.profiles = profiles;
      }
  
  @OneToMany(fetch=FetchType.LAZY, mappedBy="profileOptionElement")
      public Set&lt;TargetHasProfileOptionElement&gt; getTargetHasProfileOptionElements() {
          return this.targetHasProfileOptionElements;
      }
  
      public void setTargetHasProfileOptionElements(Set&lt;TargetHasProfileOptionElement&gt; targetHasProfileOptionElements) {
          this.targetHasProfileOptionElements = targetHasProfileOptionElements;
      }
  
  
  
  
  }
  </code></pre>
  
  <p>--</p>
  
  <p>ProfileOption.java: </p>
  
  <p>--</p>
  
  <pre><code>package models.classes_hibernate;
  // Generated 02/07/2014 10:54:27 by Hibernate Tools 3.6.0
  
  
  import javax.persistence.*;
  import java.util.HashSet;
  import java.util.Set;
  
  import static javax.persistence.GenerationType.IDENTITY;
  
  /**
   * ProfileOption generated by hbm2java
   */
  @Entity
  @Table(name="profile_option"
      ,catalog="fairtime"
  )
  public class ProfileOption  implements java.io.Serializable {
  
  
       private Integer profileOptionId;
       private String name;
       private boolean hiddenForUser;
       private boolean hiddenForAdvertiser;
       private boolean hiddenForOffer;
       private String type;
       private Boolean isUniqueForProfile;
       private Boolean isUniqueForTarget;
       private Set&lt;Offer&gt; offers = new HashSet&lt;Offer&gt;(0);
       private Set&lt;ProfileOptionElement&gt; profileOptionElements = new HashSet&lt;ProfileOptionElement&gt;(0);
  
      public ProfileOption() {
      }
  
  
      public ProfileOption(String name, boolean hiddenForUser, boolean hiddenForAdvertiser, boolean hiddenForOffer) {
          this.name = name;
          this.hiddenForUser = hiddenForUser;
          this.hiddenForAdvertiser = hiddenForAdvertiser;
          this.hiddenForOffer = hiddenForOffer;
      }
      public ProfileOption(String name, boolean hiddenForUser, boolean hiddenForAdvertiser, boolean hiddenForOffer, String type, Boolean isUniqueForProfile, Boolean isUniqueForTarget, Set&lt;Offer&gt; offers, Set&lt;ProfileOptionElement&gt; profileOptionElements) {
         this.name = name;
         this.hiddenForUser = hiddenForUser;
         this.hiddenForAdvertiser = hiddenForAdvertiser;
         this.hiddenForOffer = hiddenForOffer;
         this.type = type;
         this.isUniqueForProfile = isUniqueForProfile;
         this.isUniqueForTarget = isUniqueForTarget;
         this.offers = offers;
         this.profileOptionElements = profileOptionElements;
      }
  
       @Id @GeneratedValue(strategy=IDENTITY)
  
  
      @Column(name="profile_option_id", unique=true, nullable=false)
      public Integer getProfileOptionId() {
          return this.profileOptionId;
      }
  
      public void setProfileOptionId(Integer profileOptionId) {
          this.profileOptionId = profileOptionId;
      }
  
  
      @Column(name="name", nullable=false)
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      @Column(name="hidden_for_user", nullable=false)
      public boolean isHiddenForUser() {
          return this.hiddenForUser;
      }
  
      public void setHiddenForUser(boolean hiddenForUser) {
          this.hiddenForUser = hiddenForUser;
      }
  
  
      @Column(name="hidden_for_advertiser", nullable=false)
      public boolean isHiddenForAdvertiser() {
          return this.hiddenForAdvertiser;
      }
  
      public void setHiddenForAdvertiser(boolean hiddenForAdvertiser) {
          this.hiddenForAdvertiser = hiddenForAdvertiser;
      }
  
  
      @Column(name="hidden_for_offer", nullable=false)
      public boolean isHiddenForOffer() {
          return this.hiddenForOffer;
      }
  
      public void setHiddenForOffer(boolean hiddenForOffer) {
          this.hiddenForOffer = hiddenForOffer;
      }
  
  
      @Column(name="type", length=45)
      public String getType() {
          return this.type;
      }
  
      public void setType(String type) {
          this.type = type;
      }
  
  
      @Column(name="is_unique_for_profile")
      public Boolean getIsUniqueForProfile() {
          return this.isUniqueForProfile;
      }
  
      public void setIsUniqueForProfile(Boolean isUniqueForProfile) {
          this.isUniqueForProfile = isUniqueForProfile;
      }
  
  
      @Column(name="is_unique_for_target")
      public Boolean getIsUniqueForTarget() {
          return this.isUniqueForTarget;
      }
  
      public void setIsUniqueForTarget(Boolean isUniqueForTarget) {
          this.isUniqueForTarget = isUniqueForTarget;
      }
  
  @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="offer_has_profile_option", catalog="fairtime", joinColumns = { 
          @JoinColumn(name="profile_option_profile_option_id", nullable=false, updatable=false) }, inverseJoinColumns = { 
          @JoinColumn(name="offer_offer_id", nullable=false, updatable=false) })
      public Set&lt;Offer&gt; getOffers() {
          return this.offers;
      }
  
      public void setOffers(Set&lt;Offer&gt; offers) {
          this.offers = offers;
      }
  
  @OneToMany(fetch=FetchType.LAZY, mappedBy="profileOption")
      public Set&lt;ProfileOptionElement&gt; getProfileOptionElements() {
          return this.profileOptionElements;
      }
  
      public void setProfileOptionElements(Set&lt;ProfileOptionElement&gt; profileOptionElements) {
          this.profileOptionElements = profileOptionElements;
      }
  
  
  
  
  }
  </code></pre>
  
  <p>--</p>
  
  <p>When I tried to use hibernate I get the following exception:</p>
  
  <pre><code>Unexpected exception[PersistenceException: [PersistenceUnit: fairtimePersistenceUnit] Unable to build Hibernate SessionFactory]
  
  Caused by: org.hibernate.AnnotationException: A Foreign key refering models.classes_hibernate.ProfileOptionElement from models.classes_hibernate.Profile has the wrong number of column. should be 2
  </code></pre>
  
  <p>I cannot find the problem, thanks in advance for your help</p>