0 Replies Latest reply on Feb 15, 2007 7:05 PM by jimk1723

    Generics and Composite Keys

    jimk1723

      I have a situation where a set of entities share a similar composite key class; rather than create a new PK class for each entity, I wanted to genericise the PK.

      Here's my generic PK class:

      @Embeddable
      public class LanguagePK<T> implements Serializable {
      
       private T describes;
      
       private String code;
      
       @Column(name = "LANG_CODE")
       public String getCode() {
       return this.code;
       }
      
       public void setCode(String code) {
       this.code = code;
       }
      
       public T getDescribes() {
       return this.describes;
       }
      
       public void setDescribes(T described) {
       this.describes = described;
       }
      }
      


      ...and here's a sample entity that embeds it (Foo is another entity)

      @Entity
      @Table(name = "FOO_LANG")
      public class FooLanguage implements Serializable{
       LanguagePK<Foo> pk;
      
       @EmbeddedId()
       @AttributeOverrides( { @AttributeOverride(name = "describes", column = @Column(name = "FOO_ID")) })
       public LanguagePK<Foo> getPk() {
      
       if (pk == null) {
       pk = new LanguagePK<Foo>();
       }
      
       return pk;
       }
      
       public void setPk(LanguagePK<Foo> pk) {
       this.pk = pk;
       }
      
       @Transient
       public Foo getFoo() {
       return getPk().getDescribes();
       }
      
       public void setFoo(Foo e) {
       getPk().setDescribes(e);
       }
      
       @Transient
       public String getCode() {
       return getPk().getCode();
       }
      
       public void setCode(String code) {
       getPk().setCode(code);
       }
      }
      


      I'm getting the "Property X has an unbound type and no explicit target entity" exception when I try to use it.

       [testng] javax.persistence.PersistenceException: java.lang.IllegalStateException: Property describes has an unbound type and no explicit target entity.
       [testng] at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:217)
       [testng] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
       [testng] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
       [testng] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
      


      I've poked around, but I haven't found any example code on how to do something like this. Am I'm trying to do something I shouldn't?