title: Extend entity classes with composite keys in hibernate
body: <p>In our company we have a strange database model which can't be modified because to many systems works with them. Up to know we have a straight java application which connects with hibernate to the database and loads the data. We have for each table one xml mapping file.</p>
  
  <p>The strange thing about the database is that we do not have any primary keys. Most table have a unique index containing several columns.</p>
  
  <p>Now we want to use an application server (jboss) and the ejb model. So I created a class like this:</p>
  
  <pre><code>   @Entity
     @Table (name = "eakopf_t")
     public class Eakopf implements Serializable {
  
         @Embeddable
         public static class EakopfId implements Serializable { 
  
              private String mandant;
  
              private String fk_eakopf_posnr;
  
              // I removed here the getters and setters to shorten it up
  
         }
  
         @Id
         private EakopfId id;
  
         private String login;
  
         // I removed the getters and setters here as well    
     }
  </code></pre>
  
  <p>This works perfect.</p>
  
  <p>Because our customers have different versions of the database schema I thought about extending this class on each database release change. So each interface we create with java can decide which version of the table will be used.</p>
  
  <p>Here is the extended table class</p>
  
  <pre><code>   @Entity
     @Table (name = "eakopf_t")
     public class Eakopf6001 extends Eakopf implements Serializable {
  
          private String newField;
  
          // getters and setters
     }
  </code></pre>
  
  <p>If I use Eakopf (the base version) it is working if I do something like that:</p>
  
  <pre><code>EakopfId id = new EakopfId();
  id.setMandant("001");
  id.setFk_eakopf_posnr("ABC");
  Eakopf kopf = (Eakopf) em.find(Eakopf.class, id);
  </code></pre>
  
  <p>But if I do this:</p>
  
  <pre><code>EakopfId id = new EakopfId();
  id.setMandant("001");
  id.setFk_eakopf_posnr("ABC");
  Eakopf6001 kopf = (Eakopf6001) em.find(Eakopf6001.class, id);
  </code></pre>
  
  <p>this exception occues</p>
  
  <pre><code>javax.ejb.EJBException: javax.persistence.PersistenceException: 
  org.hibernate.WrongClassException: Object with id: 
  de.entity.Eakopf$EakopfId@291bfe83 was not of the specified subclass:  
  de.entity.Eakopf (Discriminator: null)
  </code></pre>
  
  <p>Does anybody has an idea?</p>
  
  <p>many greetings,
  Hauke</p>