title: JPA @MapsId inserting incorrect column name
body: <p>I am having trouble getting @MapsId to work, despite following all the examples on the net.</p>
  
  <p>Here is my the setup:</p>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
      &lt;version&gt;4.3.5.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;mysql&lt;/groupId&gt;
      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;version&gt;5.1.31&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>DDL scripts:</p>
  
  <pre><code>CREATE TABLE Subscription (
      id bigint not null primary key auto_increment,
      active boolean not null,
      maxNumUsers int not null
  )ENGINE=InnoDB;
  
  CREATE TABLE t.Project (
      name varchar(32) not null,
      subscriptionId bigint not null
  )ENGINE=InnoDB;
  
  ALTER TABLE t.Project ADD CONSTRAINT `projectPK` PRIMARY KEY (name, subscriptionId);
  ALTER TABLE t.Project ADD CONSTRAINT `projectSubscriptionFK` FOREIGN KEY (subscriptionId) REFERENCES t.Subscription(id);
  </code></pre>
  
  <p>ProjectId.java:</p>
  
  <pre><code>@Embeddable
  public class ProjectId implements Serializable {
      private Long subscriptionId;
      private String name;
  </code></pre>
  
  <p>Project.java:</p>
  
  <pre><code>@Entity
  @Table(schema = "t")
  public class Project {
      @EmbeddedId
      private ProjectId id;
  
      @ManyToOne
      @MapsId("subscriptionId")
      //@JoinColumn(name = "subscriptionId", referencedColumnName = "id", insertable = false, updatable = false)
      private Subscription subscription;
  </code></pre>
  
  <p>Subscription.java:</p>
  
  <pre><code>@Entity
  @Table(schema = "t")
  public class Subscription {
  
      @OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL)
      private Map&lt;ProjectId, Project&gt; projects = new HashMap&lt;&gt;();
  </code></pre>
  
  <p>When I do an insert, the following error happens:</p>
  
  <blockquote>
    <p>Hibernate: insert into t.Project (name, subscription_id) values (?, ?)
    2014-07-20 11:06:59,193
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1054,
    SQLState: 42S22 2014-07-20 11:06:59,193
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column
    'subscription_id' in 'field list'</p>
    
    <p>...</p>
    
    <p>... 94 more Caused by:
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
    column 'subscription_id' in 'field list'  at
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      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:1062)    at
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)    at
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)    at
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)     at
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)  at
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)   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:187)
      ... 114 more</p>
  </blockquote>
  
  <p>If I uncomment the <code>@JoinColumn</code> on <em>Project.subscription</em> and redeploy I get this error:</p>
  
  <blockquote>
    <p>... 21 more Caused by: org.hibernate.MappingException: Repeated column
    in mapping for collection: t.Subscription.projects column:
    subscriptionId</p>
  </blockquote>
  
  <p>I've also tried removing the <code>@MapsId</code>, I still get the repeated column error.</p>
  
  <p>I am stumped as to how to get around this. Any suggestions would be appreciated.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>Update: If I make Subscription.projects a list instead of a map and I add the JoinColumn to project.subscription it works. However if I change subscription.projects back to a map, I start getting the org.hibernate.MappingException: Repeated column again. Obviously I would really like to use the Map so I'll keep digging.</p>