title: HIbernate provided id of the wrong type, expected PK got Entity
body: <p>I am upgrading an existing Hibernate project from xml mappings to annotations. </p>
  
  <p>I have a simple example which works with xml mappings but I get following error with my annotations:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: Provided id of the wrong type for class hib.StrategyRevision. Expected: class hib.StrategyRevisionPK, got class hib.StrategyRevision
      at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:196)
      at hib.Main.main(Main.java:34)
  Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class hib.StrategyRevision. Expected: class hib.StrategyRevisionPK, got class hib.StrategyRevision
      at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:109)
      at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
      at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
      at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
      at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
      ... 1 more
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/LZe1j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LZe1j.png" alt="Tables"></a>
  Here's the original working xml mapping file (simplified for this question):</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping package="hib"&gt;
  
       &lt;class name="PortfolioRevision" table="PortfolioRevisions"&gt;
          &lt;id name="revisionId" column="RevisionId" unsaved-value="any"&gt;
              &lt;generator class="identity"/&gt;
          &lt;/id&gt;
      &lt;/class&gt;
  
      &lt;class name="Strategy" table="PortfolioStrategies"&gt;
          &lt;id name="id" column="StrategyId" unsaved-value="any"&gt;
              &lt;generator class="identity"/&gt;
          &lt;/id&gt;
      &lt;/class&gt;
  
     &lt;class name="StrategyRevision" table="StrategyRevisions"&gt;
          &lt;composite-id&gt;
              &lt;key-many-to-one name="portfolioRevision" class="PortfolioRevision" column="PortfolioRevisionId"/&gt;
              &lt;key-many-to-one name="strategy" class="Strategy" column="StrategyId"/&gt;
          &lt;/composite-id&gt;        
      &lt;/class&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Here's my annotated classes:</p>
  
  <pre><code>@Entity
  @Table(name="PortfolioRevisions")
  public class PortfolioRevision {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int revisionId;
  
      public int getRevisionId() {
          return revisionId;
      }
  
      public void setRevisionId(int revisionId) {
          this.revisionId = revisionId;
      }        
  }
  
  
  
  @Entity
  @Table(name="PortfolioStrategies")
  public class Strategy {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="StrategyId")
      private int id;
  
      public Strategy() {
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  }
  
  
  @Entity
  @Table(name="StrategyRevisions")
  @AssociationOverrides({
      @AssociationOverride(name="pk.portfolioRevision", joinColumns={@JoinColumn(name="PortfolioRevisionId")}),
      @AssociationOverride(name="pk.strategy", joinColumns={@JoinColumn(name="StrategyId")})
  })
  public class StrategyRevision implements java.io.Serializable {
      private static final long serialVersionUID = -8792535918405463902L;
  
      @EmbeddedId
      private StrategyRevisionPK pk = new StrategyRevisionPK();        
  
      @Transient
      public Strategy getStrategy() {
          return pk.getStrategy();
      }
  
      public void setStrategy(Strategy strategy) {
          this.pk.setStrategy(strategy);
      }
  
      @Transient
      public PortfolioRevision getPortfolioRevision() {
          return pk.getPortfolioRevision();
      }
  
      public void setPortfolioRevision(PortfolioRevision portfolioRevision) {
          this.pk.setPortfolioRevision(portfolioRevision);
      }
  }
  
  
  
  @Embeddable
  public class StrategyRevisionPK implements java.io.Serializable {
      private static final long serialVersionUID = 8058688937408537945L;
  
      @ManyToOne
      private PortfolioRevision portfolioRevision;
  
      @ManyToOne
      private Strategy strategy;
  
      public StrategyRevisionPK() {}
  
      public PortfolioRevision getPortfolioRevision() {
          return portfolioRevision;
      }
  
      public void setPortfolioRevision(PortfolioRevision portfolioRevision) {
          this.portfolioRevision = portfolioRevision;
      }
  
      public Strategy getStrategy() {
          return strategy;
      }
  
      public void setStrategy(Strategy strategy) {
          this.strategy = strategy;
      }
  
      // override &amp; equals...
  }
  </code></pre>
  
  <p>Main method which fails on the find:</p>
  
  <pre><code>public static void main(String[] args) {
      EntityManagerFactory factory = Persistence.createEntityManagerFactory("list");
      em = factory.createEntityManager();
      em.getTransaction().begin();
  
      PortfolioRevision portfolioRevision = new PortfolioRevision();
      em.persist(portfolioRevision);
  
      Strategy strategy = new Strategy();
      em.persist(strategy);
  
      StrategyRevision strategyRevision = new StrategyRevision();
      strategyRevision.setPortfolioRevision(portfolioRevision);
      strategyRevision.setStrategy(strategy);        
      em.persist(strategyRevision);
  
      // TEST
      StrategyRevision strategyRevision2 = new StrategyRevision();
      strategyRevision2.setStrategy(strategy);
      strategyRevision2.setPortfolioRevision(portfolioRevision);
      strategyRevision2 = em.find(StrategyRevision.class, strategyRevision);               
      em.getTransaction().commit();      
  }
  </code></pre>
  
  <p>If someone could correct the mistakes in my annotations so they work in same way as existing xml mapping I would be in their debt. I'm using Hibernate 3.3.1.GA
  Thanks.</p>